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

Can you provide a custom package for me?

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

Redirect URLs using .htaccess

Sometimes you need to redirect some URL and/or page on your site to another one.The feature is...

Change PHP variables using .htaccess

If you need to change the way your PHP is working you can do that using .htaccess.Please, note...

Block Bad robots, spiders, crawlers and harvesters

There are lots of examples across the internet that use ModRewrite. We will provide such an...

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...