Search code examples
.htaccessmod-rewritehttpsexpressionengine

htaccess: turning https on and off for specific urls?


I am trying to configure some conditional redirects, what I want is this:

http://www.example.com/login
http://www.example.com/login/*
http://www.example.com/login/account
http://www.example.com/login/account/*
https://www.example.com/account
https://www.example.com/account/*

My problem is the current .htaccess rules are forcing the /login/account to run under https so I get this:

http://www.example.com/login
http://www.example.com/login/*
https://www.example.com/login/account
https://www.example.com/login/account/*
https://www.example.com/account
https://www.example.com/account/*

Here's the current rules:

<Files .htaccess>
 order allow,deny
 deny from all
</Files>

<IfModule mod_rewrite.c> 
RewriteEngine On 

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ - [E=site_url:http://www.example.com]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ - [E=site_url:https://www.example.com]

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

RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(/account) 
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]

I guess the problem is with the RewriteCond %{REQUEST_URI} !(/account) rule. I've tried numerous variations and searched everywhere but haven't found anything that matches what I want to do.

So just to clarify everthing below /account must be https everything else should be http.

For info this is an expression engine site so these are not physical files or directories but dynamically generated urls.

Any help would be much appreciated.


Solution

  • Try this...

    RewriteCond %{SERVER_PORT} 80
    RewriteCond %{REQUEST_URI} /account
    RewriteCond %{REQUEST_URI} !(login/account)
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
    
    RewriteCond %{SERVER_PORT} 443
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(/account) 
    RewriteCond %{REQUEST_URI} (login/account)
    RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
    

    ExpressionEngine specific example of HTTPS/HTTP: https://expressionengine.stackexchange.com/questions/7601/adding-a-htaccess-redirect-to-https-that-plays-nicely-with-existing-ee-htacces/7659#7659