Skip to content
Advertisement

How to force HTTP to HTTPS with an exception?

So I had to force all connections via HTTP to go to HTTPS on a specific folder (e.g. “public_html/folder1”). I found a tutorial somewhere and used the following in an .htaccess file:

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

Problem is, I have a folder inside that folder1 which should be allowed to be accessed via HTTP and not just HTTPS, how do I make an exception? (meaning, I want for instance folder1/subfolder1 to be allowed to be accessed via HTTP).

Thanks

Advertisement

Answer

Try this :

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} ^/folder/((?!subfolder).*)$ 

RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]

The pattern ^/folder/((?!subfolde).*) matches the request uri string /folder/.* and it doesnt match when the string is /folder/subfolder/.* so the rule is not applied.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement