proper redirect taking into consideration http vs https

proper redirect taking into consideration http vs https - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


This seems to be such a standard question, but I am not able to find the answer anywhere. I am trying to solve the famous .htaccess redirect.



I have tried / modified / tested a good number of the available solutions online but they either don't work or don't do what I need.



Here is the latest one that I have:



Options +FollowSymLinks
RewriteEngine On
RewriteCond %HTTP_HOST !^www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]


This seems to work fine if I try to go from example.com to www.example.com. But example.com/test/test.html redirects to www.example.com/test.html! What? :-(



The redirect policy I am looking for - notice the difference between http and https




  • http://example.com --> http://www.example.com

  • http://example.com/test/test.html --> http://www.example.com/test/test.html

  • https://example.com --> https://www.example.com

  • https://example.com/test/test.html?a=b --> https://www.example.com/test/test.html?a=b


Solution :

You will have one problem that you will need to address so I will cover it first.



You will have trouble with duplicate content using the method you suggest. On each page, you can have either HTTP point to HTTPS content or the other way around. Adding this to the header of each page, obviously modifying the example URL on a case by case basis, then you should be fine.



For HTTPS pointing to HTTP:



<link rel="canonical" href="http://www.example.com/cake/blue-berry-ring-cakes-rock" />


-- or --



For HTTP pointing to HTTPS:



<link rel="canonical" href="https://www.example.com/cake/blue-berry-ring-cakes-rock" />


I think the following should work for you. I modified it from another answer that worked okay. If there is a problem, we can look at it again.



RewriteEngine On

RewriteCond %HTTPS off
RewriteCond %HTTP_HOST ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %HTTPS on
RewriteCond %HTTP_HOST ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]


Thanks to the hint of Max and this post, I found the answer to my problem:



RewriteEngine On
RewriteCond %HTTP_HOST !^$
RewriteCond %HTTP_HOST ^mydomain.com [NC]
RewriteCond %HTTPSs ^on(s)|
RewriteRule ^ http%1://www.%HTTP_HOST%REQUEST_URI [R=301,L]

Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect