Tag Archives: https

apache redirects to https

With no cost SSL certificates easily available, there’s no reason not to have your websites running on https. Here’s what you need to setup inside your apache conf files. (I’ll use this server as an example)


<VirtualHost *:80>
    ServerName markcerv.com
    ServerAlias www.markcerv.com
    DocumentRoot /srv/www/markcerv/htdocs/
    <Directory /srv/www/markcerv/htdocs>
        Require all granted
        Allowoverride ALL
    </Directory>

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

</VirtualHost>

The keywords that need to be added are:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

That’s it. That should force everything over to the https version of your site. With this method, if your ServerName contains www.example.com, then you’ll get redirected to the www version of the site.

Force all web traffic to redirect to SSL (https) web server using .htaccess file

Here is the minimum amount of configuration needed to force all web traffic (port 80, http) over to https (port 443 via SSL).

1) Create a .htaccess file that looks similar to this one:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*) https://www.example.com/$1 [L]

and place it at the root of the web server directory where you want to redirect the traffic. Restart apache.

2) If that doesn’t work, you might need to modify your apache config file’s AllowOverride setting. If you were trying to use a .htaccess file just inside of the /var/www directory, you might need to edit /etc/apache2/sites-enabled/[name-of-your-site]

...
   <Directory /var/www/>
        ...
        Options FollowSymLinks
        AllowOverride FileInfo
        ...
   </Directory>

NOTE: Your web site might have other directives for Options and AllowOverride, but the 2 that I have show are the bare minimum to get the .htaccess file working.