Force SSL/https using .htaccess and mod_rewrite

Sometimes you may need to make sure that the user is browsing your site over a secure connection.

An easy to way to always redirect the user to secure connection (https://) can be accomplished with a .htaccess file containing the following lines:

 

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


Please, note that the .htaccess should be located in the web site main folder.

In case you wish to force HTTPS for a particular folder you can use:

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


The .htaccess file should be placed in the folder where you need to force HTTPS.
  • 59 Users Found This Useful
Was this answer helpful?

Related Articles

Using .htaccess for password protecting your folders

If you need to have certain areas (folders or files) of your web site protected you can use...

Force www vs non-www to avoid duplicate content on Google

Recently, it has been talked a lot about Google and duplicate content as well as Google Canonical...

Can you provide a custom package for me?

Yes, most definitely. Simply open up a helpdesk ticket in the Sales department or contact us...

301 Permanent redirects for parked domain names

If you have several domain names parked/pointed at your site it is a good idea to create...

Common Apache mod_rewrite and examples

I am going to give you some common dot-htaccess codes our customers love. Please remember we do...