htaccess – How to 301 Redirect HTTP Links to HTTPS

.htaccesshttps

We are using the SSL securing the whole site now. At the admin end, unsecure and secure URL are all in the form of https.

Now the issue we are facing right now is, for example, the link "http://www.example.com/abc" is 301-redirected to "https://www.example.com". The correct way is to redirect to "https ://www.example.com/abc

Anyone can share the idea the correct way to do it?

Thanks!

Best Answer

Based on the hints by Simon, here is what I have:

1) Make sure "Auto-redirect to Base URL = No". It was set to "yes", and causing the problem.

2) Put the below in the .htaccess:

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

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

 RewriteCond %{SERVER_PORT} !^443$
 RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

It works for me, at least for now.

Related Topic