How can I redirect all pages to https, using .htaccess, with exceptions?

14,075

Solution 1

You're already part way there with your use of RewriteCond to restrict the rewrites to requests on port 80.

You do not want to rewrite requests on port 80 for /videos and /blog, so:

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/videos
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://ourdomain.com/$1 [R,L]

And you want to rewrite requests for these URLs on port (presumably) 443:

RewriteCond %{SERVER_PORT} 443
RewriteRule ^/((videos|blog)/.*) http://ourdomain.com/$1 [R,L]

Solution 2

Try

RewriteEngine on
RewriteBase /

RewriteCond %{SERVER_PORT} 443
RewriteRule ^blog/?(.*) http://ourdomain.com/blog/$1 [R,L]

RewriteCond %{SERVER_PORT} 443
RewriteRule ^videos/?(.*) http://ourdomain.com/videos/$1 [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/videos
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://ourdomain.com/$1 [R,L]
Share:
14,075
Martin Hunt
Author by

Martin Hunt

Updated on June 04, 2022

Comments

  • Martin Hunt
    Martin Hunt almost 2 years

    I have used htaccess to force all pages to re route to https using...

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

    Is there any way I can reverse that rule for ourdomain.com/videos && ourdomain.com/blog making sure that these pages are forced to drop the https and use http?

  • Martin Hunt
    Martin Hunt almost 12 years
    Thanks for the help, I'm getting spme strange behaviour with both solutions... If I go to domain.com/blog or /videos it doesnt redirect and keeps the https if I go to domain.com/videos or /blog it directs back to the homepage domain.com