HTTPS redirect doesn't appear to follow .htaccess RewriteRule, how can I fix this?
I am redirecting example.com
to sample.com/?example
, so that I can have a modal popup saying, example has changed its name to sample.
It works fine for HTTP redirects from http://example.com
, and almost everything works for HTTPS redirects from https://example.com
, except for these two things (applying only to the redirects from https://example.com
):
In the address bar, it will still say
https://example.com
, but show the page forhttps://sample.com
. Whenever a link is clicked on this page, the problem is fixed and it then showshttps://sample.com
in the address bar.It will not keep the query that I add (
?example=1
).
Here are my .htaccess rules:
RewriteCond %HTTPS ^example.com$ [OR]
RewriteCond %HTTPS ^www.example.com$
RewriteRule (.*)$ https://www.sample.com/$1?example=1 [R=301,L]
How can I fix these so that when I go to https://example.com
, it goes to https://sample.com?example=1
You must have more directives in your .htaccess in order to do the complete HTTP and HTTPS redirection. So, filling in the gaps...
For the sake of simplicity I've assumed you don't have any other subdomains (apart from www
). So you are redirecting everything at example.com
to www.sample.com
.
# Redirect http://example to http://sample
RewriteCond %HTTPS off
RewriteCond %HTTP_HOST example.com
RewriteRule (.*) http://www.sample.com/$1?example=1 [R=301,L]
# Redirect https://example to https://sample
RewriteCond %HTTPS on
RewriteCond %HTTP_HOST example.com
RewriteRule (.*) https://www.sample.com/$1?example=1 [R=301,L]
%HTTPS
is always set (regardless of whether SSL is enabled or not) and simply contains the value "on" or "off".
The trailing $
(end of pattern marker) on the RewriteRule
pattern is not required. And the 2nd argument to RewriteCond
(the CondPattern) is a regular expression (most of the time) so the dots should really be escaped, otherwise they match anything.
Comments
Post a Comment