HTTPS redirect, exclude script parameters from URL
My goals:
1. http://example.com/page/index.php -> https://example.com/page/
2. http://example.com/page/ -> https://example.com/page/
3. http://www.example.com/page/ -> https://example.com/page/
4. Any nonexistent file or directory requests -> index.php?&q=$1 for further processing.
The following rules work fine, but only after a redirect in the browser URL instead of a clean URL. For example, if I request:
https://example.com/page/
I see
https://example.com/index.php?&q=page/
How do I hide index.php?&q=page
and show https://example.com/page/
in the URL instead? Before I added HTTPS redirect it worked fine for HTTP. I tried 2 solutions with the same result.
RewriteEngine on
RewriteBase /
Solution 1:
#1 solution:
RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]
RewriteCond %SERVER_PORT 80 [OR]
RewriteCond %HTTP_HOST ^www. [NC]
RewriteCond %HTTP_HOST ^(?:www.)?(.+)$ [NC]
RewriteRule (.*) https://%1%REQUEST_URI [L,NE,R=301]
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule (.*) index.php?&q=$1 [QSA]
Solution 2:
#2 solution:
RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]
RewriteCond %HTTP_HOST ^www.(.*)$ [NC]
RewriteRule (.*) https://%1%REQUEST_URI [L,NE,R=301]
RewriteCond %SERVER_PORT 80
RewriteRule (.*) https://%HTTP_HOST%REQUEST_URI [L,NE,R=301]
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule (.*) index.php?&q=$1 [QSA]
How do I hide
index.php?&q=page
and showhttps://example.com/page/
in the URL instead? Before I added HTTPS redirect it worked fine for HTTP.
The code you posted should not result in such a "redirect". And neither should adding the "HTTPS redirect" have affected this.
Unless... you initially added the "HTTPS redirect" at the end of your .htaccess
file, after the internal rewrite to index.php?&q=$1
. In which case, you would have expected a request for either http://example.com/page/
or https://www.example.com/page/
or http://www.example.com/page/
to have resulted in such an external redirect (as opposed to an internal rewrite) to https://example.com/index.php?&q=page/
. HOWEVER, you have stated that requesting https://example.com/page/
(already HTTPS and non-www) results in such a redirect - that would not happen under this scenario.
It's still possible that an earlier (erroneous) redirect has been cached (301's are persistently cached by the browser) and this is what you are seeing here. However, you've stated (in comments) that the (browser? and all intermediary?) cache(s) has been cleared. So, that just leaves "something else" (ie. something else external to the directives you've posted that sets a 3xx HTTP status)?
Additional notes, not directly related to your main problem:
http://example.com/page/index.php
->https://example.com/page/
RewriteCond %THE_REQUEST ^[A-Z]3,9 /index.php HTTP/ [NC]
RewriteRule ^index.php$ https://%HTTP_HOST/ [L,NE,R=301]
Condition #1 is not satisfied by any of the directives you've posted. The directives you've posted above would only redirect a request for /index.php
in the document root, not /page/index.php
as you've stated. To redirect /page/index.php
, you would naturally need to include the /page
path segment when matching the requested URL (and pass this through to the substitution). For example:
RewriteCond %THE_REQUEST ^[A-Z]3,9 /page/index.php HTTP/ [NC]
RewriteRule ^(page/)index.php$ https://%HTTP_HOST/$1 [L,NE,R=301]
However, I suspect an "any directory" solution would be required. In which case you could do something like the following:
RewriteCond %THE_REQUEST /index.php HTTP/ [NC]
RewriteRule (.*)index.php$ https://%HTTP_HOST/$1 [L,NE,R=301]
RewriteRule (.*) index.php?&q=$1 [QSA]
You should probably include the L
(last
) flag on this directive, in case you add any more directives later in the file. (This would have prevented an erroneous redirect if you had added the HTTPS redirect at the end by mistake.)
And, unless you have a specific requirement, you should be rewriting to index.php?q=$1
(no &
after the ?
). For example:
RewriteRule (.*) index.php?q=$1 [QSA,L]
Comments
Post a Comment