Apache Redirect HTTP to HTTPS using mod_rewrite

Apache’s mod_rewrite makes it easy to require SSL to be used on your site and to gently redirect users who forget to add the https when typing the URL. Using Apache to redirect http to https will make sure that your site (or a part of it) will only be accessed by your customers using SSL.

Before you can set up an Apache redirect from http to https, make sure below prerequisites are taken care.

  1. Make sure your SSL certificate is successfully installed so you can access https://www.yoursite.com
  2. Make sure mod_rewrite is enabled in Apache
  3. Make sure your Apache instance is listening on HTTP port 80

Now you just need to edit your httpd.conf file or the file where your virtual host is specified and add these lines to redirect http to https:

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

Now, when a visitor types http://www.yoursite.com the server will automatically redirect http to https so that they go to https://www.yoursite.com

<<Click here to see all posts>>

If you found any of the information on this page helpful in anyway then please consider sharing this content with your favorite social network or by leaving your thoughts in the comment section. Thanks!

Leave a Reply

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

Prove You Are Human Time limit is exhausted. Please reload CAPTCHA.