Search code examples
.htaccesshttp-redirecthttp-status-code-301

http to https double redirection found


I am trying to redirect http to https with three options

However, the second option is showing double redirect from http://www > https://www > https://

Following is the code I have in .htaccess. Is it fine to have a double redirect?

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
   RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

Solution

  • SInce you have 2 redirection rules. A redirection may happen twice as:

    1. Using first rule http://www.example.com to https://www.example.com
    2. Using second rule https://www.example.com to https://example.com

    You can use this single rule to do both in a single 301 redirect:

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

    Make sure you replace both of your rules with this one and use a new browser for testing to avoid old browser cache.