Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 Redirect HTTP to HTTPS: Best Way

I have been searching for the perfect 301 redirect. But I am finding so many solutions and do not know which one is the best. Though the top 2 option I have tried and they are working.

This is for a completely new site where I want to redirect everything from http://www.exampledomain.com to https://www.exampledomain.com

Would be great if someone help me out on this one.

I am finding many references, as follows:

1. <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R,L] </IfModule>

2. RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. <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>

4. RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

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

6. RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} example\.com$ [NC] RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

Thanks a ton, Jay

like image 305
Mrutyunjaya Avatar asked Dec 13 '25 23:12

Mrutyunjaya


1 Answers

They all are pretty the same, only with small variantions.

  1. detects non-https traffic by port number (80), not by variable %{HTTPS}, and redirects only to https://www.exampledomain.com/. So skip this one.

  2. This is the "core" version, it says "when the request is http-only, redirect it to adequate https version". Nothing more, nothing less.

  3. Similar to 2, but also adds condition whether mod_rewrite is available in Apache. You don't need to add it if you are sure (without mod_rewrite, none of these methods will work anyway). And also, it strips "www" on beginning of domain name, so all traffic is redirected to non-www version of the website (from www.somedomain.com to somedomain.com)

  4. This one is missing [L,R=301], it was probably omitted by mistake

  5. Similar to 2, but redirects to www version of domain name.

  6. Similar to 2, but it is not generic, works only for example.com (which you don't want to use in real world).

So, pick 2 if you don't want to fiddle with www in address, or pick 3 or 5 if you do. You can add RewriteEngine On on beginning of .htaccess, it is needed onlu once, not for every rule. And you can add the <IfModule mod_rewrite.c> and </IfModule> from 3. if you want to use the same code on different servers and you are not sure which modules will be available.

like image 68
Ondra Koupil Avatar answered Dec 16 '25 14:12

Ondra Koupil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!