Activities

June 2015
M T W T F S S
« May   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  

How to force non http request to https using .htaccess

I had a very busy life schedule and so has limited time for improving knowledge session. Here what I’m found useful if you have a need of protecting a website over SSL.

The following rules can serve two purpose,

1. It will re-direct all the non http traffic to https by appending www prefix (http://test.com –> https://www.test.com)
2. It will force all secure domain access to secure www prefix domain name (https://test.com —> https://www.test.com)

#Moving all  http traffic to https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.test.com/$1 [R,L]

#Moving secure non www domain to secured www domains
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

If you are using Amazon Elastic load balancer (ELB) the following rules work fine

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>