0

I recently migrated my website from HTTP to HTTPS. I read on various websites about redirecting all HTTP requests to HTTPS using .htaccess codes.

Lots of websites provide different codes and now I'm confused which code should I use.

Following are 4 codes found on various websites:

1st code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]

2nd code:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

3rd code:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

4th code:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://secure.example.com/
</VirtualHost>

<VirtualHost _default_:443>
ServerName secure.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
</VirtualHost>

Please guide me which code is perfect and I should put in my .htaccess file?

1 Answers1

1

For starters: the last configuration snippet is not even valid in .htaccess files...

(But since you shouldn't be using .htaccess files anyway if you're a system administrator that's not an issue.)

Redirecting http to https a text book example of when (as an admin) you should not need a mod_rewrite approach and use a simple redirect instead. So, if you do have access to the main server config, your last snippet with a simple redirect from the plain http VirtualHost is recommended.

HBruijn
  • 84,206
  • 24
  • 145
  • 224