19. Feb 2018 17:48 by Ivan • Snippets • # # # # # #

Redirect all HTTP requests to HTTPS

If you just acquired your SSL certificate and set it up, chances are that you will need to force the website to use secure HTTPS version by default. This is done very easily, you’ll just need to create .htaccess file (if it doesn’t exist already)  in root folder of your website, and on top of the file add this 3 lines of code:

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

and that’s basically it. RewriteCond %{HTTPS} off excludes https requests, which means, if visitor is entering website with https already, it wont do the next line of code, which is actual redirection, because there is no need for it. When adding these lines I suggest that you don’t forget to add R=301 flag, which informs the crawlers that this is permanent redirection, not temporary one.

Now with that being said, this is not the recommended way of making http to https redirection. Apache documentation recommend using Rewrite directive in virtual hosts files instead, if you of course have the access to all server files:

<VirtualHost *:80>
  ServerName www.example.com
  Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
  ServerName www.example.com
  # ... SSL configuration goes here
</VirtualHost>

As I’ve said this is recommended way only if you have access to specific files, which you won’t have on most of the shared hostings. In that case use of RewriteRule in .htaccess is more than fine.

2 responses to “Redirect all HTTP requests to HTTPS”

  1. Wonderful paintings! This is the kind of info that are meant to be shared around the web. Shame on the search engines for no longer positioning this submit upper! Come on over and discuss with my website . Thank you =)

  2. I conceive other website owners should take this internet site as an example , very clean and good user pleasant pattern.

Leave a Reply

Your email address will not be published. Required fields are marked *

Redirect all HTTP requests to HTTPS

by Ivan time to read: 1 min
2