Mobile app version of vmapp.org
Login or Join
Ravi8258870

: Proper redirect taking into consideration http vs https 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

@Ravi8258870

Posted in: #301Redirect #Htaccess #Redirects

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 ^(.*)$ www.example.com/ [R=permanent,L]


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

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

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

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Ravi8258870

2 Comments

Sorted by latest first Latest Oldest Best

 

@Speyer207

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 %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

10% popularity Vote Up Vote Down


 

@Ann8826881

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 ^(.*)$ www.example.com/ [R=301,L]

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme