How to Force HTTP Protocol on directories of your Secure website using .htaccess file

0
1392
force http instead of https working solution

Recently, I came across a situation where I wanted to have some parts of my website https secure and other parts be http.

force http instead of https working solution

So, after looking at internet everybody tries to force marry you with SSL which is not a bad thing but if you want to choose between http and https it should be your decision not every page requires SSL and also if you are using Adsense its much better to use http instead of https because not all advertisers have https compliant ads meaning if you are serving ads on your https pages its likely their will be less competition for your ad spot and many times it will go blank. And, I have experienced this myself http pages serve ads 95-99% of the times whereas pages with https range between 70-80%. Enough talking lets look at How I figured out Solution to this problem.

For my domain: mrvirk.com, the homepage is forced https SSL, mail whereas the qr tool and blog are force http.

I have divided each section of the website as a folder as such.

Root folder: /

Mail folder: /mail

Qr folder: /qr

Blog folder: /blog

This way, I can have separate .htaccess file for each folder and simply control if the content/pages of this folder will be http or https.

 

To Force SSL a folder (change “foldername” to your folder name):

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

 

Now Some 100% Working Examples:

To Force SSL (root folder)

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

Result:


To Force SSL (/mail folder):

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

Result:

force https
Force HTTPS Example

 

To Force HTTP a folder:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Result:

force http
Force HTTP

 

how to force http but not https
Force http but not https

 

 

Tags: Force HTTP instead of HTTPS in .htaccess file, Force HTTP, Force HTTPS, Force HTTP Working

Please Note: the folders/sites may change b/w http and https anytime but the code will still stay relevant

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.