Force SSL on Apache

Redirecting your Apache web server to serve pages over SSL instead of normal HTTP protocol is easy. Just edit your server block for port 80 (or whatever port you have set up to serve normal unencrypted traffic), and make it look like this snippet of code below.

redirect to ssl

Your normal virtual host file, that serves connections on port 80 should look like this. We need to change it to redirect normal traffic to port 443 (your SSL port).

     
     ServerAdmin [Webmasters's Email Address]
     Directory /Directory/of/Website/
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        

To redirect traffic to port 443, delete everything in the server block. Then replace it with the code below.

        
        RewriteEngine on
        ReWriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.