Htaccess redirects adds question mark to url

.htaccessredirect

I have the following in my htaccess:

#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?/$1 [L]

#No WWW
RewriteCond %{HTTP_HOST} !^myrealdomain.com$ [NC]
RewriteRule ^(.*)$ https://myrealdomain.com/$1 [L,R=301]

#Always redirect to SSL pages
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

I want the www to be redirected and I want all pages to be https. It seems to work, but when I try to go to https://www.d.com/hello it redirects to https://d.com/?/hello.

Where did that extra question mark come from?

In addition, creating a facebook like button seems to not like my domain at all. The button appears blank and I think its do with the above…?

Any help much appreciated.

Best Answer

This is generally a poorly understood part of mod_rewrite. All rules (unless a rule is followed by [L]) are processed in order and the rules change the value of the URI. They do not change the value of %{THE_REQUEST}.

Hence your second rule:

RewriteRule ^(.*)$ ?/$1 [L]

Causes a request for /hello to be rewritten to ?/hello. The input to the next rule is now ?/hello.

The next rule:

RewriteRule ^(.*)$ https://myrealdomain.com/$1 [L,R=301]

Uses $1 to match the entire URI and sends a 301 redirect to that URI on a new domain. That's where the question mark comes from. It's an internal rewrite followed by a redirect in the same request.

You can solve this problem by moving the ^application rewrite and the valid file rewrite to the bottom, after all the redirect rules.

Your https redirect is missing [L,R=301].

I have no idea about Facebook like buttons but after fixing the question mark problem, if it still doesn't work, try asking again or reading though the Facebook documentation.

Related Topic