How to Redirect HTTP to HTTPS with Exceptions

We have a requirement to redirect HTTP to HTTPS but we want to exclude image files from the redirection.
First of all, make sure mod_rewrite is enabled in Apache; in other words, make sure that /etc/httpd/conf/httpd.conf contains the line:

LoadModule rewrite_module modules/mod_rewrite.so

Backup httpd.conf before you add the following:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(.+)\.png$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.jpg$
RewriteCond %{REQUEST_FILENAME} !^(.+)\.gif$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

Reference:
http://corz.org/serv/tricks/htaccess2.php
http://wiki.apache.org/httpd/RewriteCond
http://www.whoopis.com/howtos/apache-rewrite.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s