Force HTTPS on Server using .htaccess

If you are familiar with the Apache web server then you must have known that the .htaccess file at the very root directory of your website plays a very important role. You can not only rewrite your site's permalink structure but actually can do far more than that. I am expecting to write lot more posts on this, Slowly but gradually.

But today I will show you how to force all of your site traffic to use the SSL or HTTPS protocol (secured connection) while someone is visiting your site.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
</IfModule>

Simply just copy this snippet and paste it on your .htaccess file. Make sure you change the "mydomain.com" with your domain name and you are all set.

One might ask, how about if I want to force https:// to a very specific folder. Let's say that folder suppose to be something like this one:

https://yourdomain.com/admin

It can be done fairly easily. This is what you should add on your .htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} admin
RewriteRule ^(.*)$ https://yourdomain.com/admin$1 [R,L]
</IfModule>

Remember that your .htaccess file should be at the very root directory of your site. From now on, all the different https:// requests (https://yourdomain.com or https://www.yourdomain.com) will be transferred to your "https://urdomain.com". Hope that helps.

Resource: mod_rewrite

Comments

Commenting is disabled.