1

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!

3

3 Answers 3

5

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.

1
  • What in case If I want to skip some URLs being redirected to HTTPS , i did added condition , but it then redirects to index.php , Any idea ? Commented Apr 12, 2018 at 13:11
1

This will redirect http to https while preserving the querystring:

RewriteCond %{HTTP_HOST} www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 
1
  • seems still not working Commented Feb 12, 2015 at 15:07
1

Try this in your .htaccess file:

############################################
## enables the usage of %{ENV:proto} as the protocol (http vs https) in the rewrite rules
    RewriteCond %{HTTPS} =on
    RewriteRule ^(.*)$ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ - [env=proto:http]

############################################
## redirect from non-www to www
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ %{ENV:proto}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It is quite generic and works for me. It also ensures that www. is always used. It is based on this StackOverflow answer.

2
  • Thanks Simon. I will try soon and will update you and everybody (who might have the same issue) the result. Commented Feb 13, 2015 at 8:08
  • Hi Simon, thank you, and your solutions work, but giving me a 302 redirection instead of 301. In addition, one important condition is to make sure "Auto-redirect to Base URL = No". Thanks for the reference answer you provided! I am going to share what I have below. Commented Feb 13, 2015 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.