ssl Htaccess redirect URL HTTP to HTTPS

Last updated on October 25th, 2014 at 09:33 am

Once you have configured SSL for your website the next step is to automatically redirect all visitors accessing URL from HTTP to HTTPS. Redirecting from HTTP to HTTPS automatically can be accomplished with the help of .htaccess file.

All you have to do is create (If you don’t have one) htaccess file in the Website root directory.

Make sure that mod_rewrite is enabled on your Apache configuration file [httpd.conf]

For example my root directory is /usr/local2/www

I am creating a .htaccess file in this location.

Add the below content,

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you already have a .htaccess file with statement “RewriteEngine On” for some other rewrite purpose then all you need to do is add the below 2 lines

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This check whether URL requested contains https, if not it will rewrite URL using the rule defined.

HTTP_HOST will be your hostname, and the REQUEST_URI will be the URI of your hostname.

[L] Last rewrite rule and stop the rule if it matches.
[R=301] means it is a permanent URL redirect
^ Caret symbol matches the start of a string/url

https://%{HTTP_HOST}%{REQUEST_URI}

For example if the URL requested by a user is
http://mistonline.in/wp/archives/html

Then internally the server will redirect this URL to HTTPS
https://mistonline.in/wp/archives/html

Where, HTTP_HOST is mistonline.in and REQUEST_URI is wp/archives/html

Leave a Reply

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