KinsDotNet KinsDotNet - 3 months ago 10
Apache Configuration Question

How can I direct all pages except one to HTTPS in an htaccess?

Currently I am redirecting all pages to https using the following htaccess directive:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]


But I have one page,
http://foo.bar/baz
which needs to be accessed http-only. Using an htaccess directive, how can I redirect to https on all pages except this one, where users are forced to http?

Edit: I should have mentioned that this is a Laravel 4 application, and I currently also have the following rules to handle the front-controller

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Answer

You can use a negative regex pattern in RewriteRule:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/baz[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /baz[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteRule ^index\.php - [L,NC]

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301,NE]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Comments