Search code examples
.htaccessrackspace-cloudrackspace

htaccess - redirect all pages except checkout to HTTP non-SSL


I'm trying to redirect all traffic to HTTP unless it is our checkout process, in which case I'm trying to redirect it to HTTPS.

I have a Rackspace Cloud Sites setup however the below does not appear to be working. Is there something that I am doing incorrect in the below?

Thanks!

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

RewriteEngine On
RewriteBase /

#redirect to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#redirect all https traffic to http, unless it is pointed at checkout
RewriteCond %{HTTP:X-Forwarded-SSL} on
RewriteCond %{REQUEST_URI} !^/checkout/?.*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

#redirect all http traffic to https, if it is pointed at /checkout
RewriteCond %{HTTP:X-Forwarded-SSL} off
RewriteCond %{REQUEST_URI} ^/checkout/?.*$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Solution

  • RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
    
    #Force NON-SSL on a non-checkout directories 
    RewriteCond %{ENV:HTTPS} on [NC]
    RewriteCond %{REQUEST_URI} !^/DIRECTORY/?.*$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
    
    #Force SSL on a specific directory
    RewriteCond %{ENV:HTTPS} !on [NC]
    RewriteRule ^DIRECTORY/(.*)$ https://www.example.com/DIRECTORY/$1 [R,L]