How to Redirect from HTTP to HTTPS in Apache

.htaccessapache-2.2

This is a canonical question about redirecting from http to https in Apache

Related:

I have an Appache web server which serves both http://example.com/ and https://example.com/. I want to redirect all http requests to https. Currently I'm using this .htaccess rule to redirect http requests to https.

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

It's working as expected for example.com but the same rule is not working for sub links, when I access existing links, like example.com/about it will still load in http, no redirection happens for existing links.

How can I make Apache redirect all http requests to https?

Best Answer

You should configure Apache Virtualhosts to do the job. RewriteMod isn't the appropriate solution for this case and .htaccess isn't either.

In your httpd.conf or equivalent use the following lines accordingly your needs. Edit it to your domain and site.

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

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

   ** Additional configurations here **

</VirtualHost>

Hope this clarifies the procedure.

Related Topic