Tuesday, January 16, 2007

Configure an apache web server to redirect http connections to https

Is easy to configure an apache web server to enable SSL access, but sometimes people tries to access the server without using SSL.

If you don't have configured any virtualserver with the same host address as your SSL one they will access to the first virtualserver host that apache will found at port 80 (non-SSL port).

To prevent this kind of access, you can configure a non-SSL virtualserver host with the SSL hostname and inside it redirect the calls to the SSL one. That will prevent your user to access the wrong server and will give you the pace of mind to know all the communication with your SSL enabled server is *really* encripted.

The easiest way to do it is using the mod_rewrite engine with the following lines at the beginning of your SSL virtualserver config file:


<VirtualHost *:80>
DocumentRoot /var/www
ServerName <full_qualified_hostname>
<Location />
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L]
</Location>

</VirtualHost>


Now all the http connections to your SSL server will be redirected to the SSL connection.

Good luck.

1 Comments:

Blogger Hemant said...

Thanks a ton! It worked for me at last with your blog help.
Just wanted to add one more thing to that is to uncomment the LoadModule for rewrite module in the httpd.conf before restarting the apache web server server.

Which looks like LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf

8:16 AM  

Post a Comment

<< Home