HTTPS redirect doesn't appear to follow .htaccess RewriteRule, how can I fix this?

HTTPS redirect doesn't appear to follow .htaccess RewriteRule, how can I fix this? - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the HTTPS redirect doesn't appear to follow .htaccess RewriteRule, how can I fix this? error on your web browser. Problem :


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):




  1. In the address bar, it will still say https://example.com, but show the page for https://sample.com. Whenever a link is clicked on this page, the problem is fixed and it then shows https://sample.com in the address bar.


  2. 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


Solution :

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.


We hope that this article has helped you resolve the htaccess, redirects, apache error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How do I redirect traffic only if being accessed from a specific port?

DNS entry to redirect constant url to changing directory name

How to redirect to any domain [duplicate]