#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Mod_rewrite http https redirect and rewrite


    Hi folks,

    I would currently like to do the following:

    1) If https is off and I'm requesting /login/ or /register/, redirect to https
    2) If https is on and I'm requesting anything other than /login/ or /register/, redirect to http
    3) Rewrite the URL (/login/, /register/, and other pages rewrite to something else)

    Code:
    RewriteCond %{HTTPS} off
    RewriteCond %{REQUEST_URI} (/login/|/register/)
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !(/login/|/register/) 
    RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
    RewriteRule ^login/?$ ?page=login
    RewriteRule ^login/?$ ?page=register
    I tested the rewrite conditions and these rules look fine. However, when I hit /login/ or /register/, my sites redirect as such:

    http://mysite.com/login/
    desired: https://mysite.com/login/
    actual: http://mysite.com/?page=login

    http://mysite.com/register/
    desired: https://mysite.com/register/
    actual: http://mysite.com/?page=register

    I tested the first 2 sets of rewrite conditions (for http and https) and they seem to work fine. For some reason, I think the last 2 rewrite rules are redirecting the desired URL to the actual URL.

    Any thoughts?
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Try including the file name in the rewritten path for the last set of rewrite rules.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by E-Oreo
    Try including the file name in the rewritten path for the last set of rewrite rules.
    Unfortunately, this yields the same results (actually had a mistake in my original code... but the correct rules w/ your suggestion would be:

    Code:
    RewriteRule ^login/?$ /index.php?page=login
    RewriteRule ^register/?$ /index.php?page=register
    Is this correct? If so, I still wasn't able to redirect to:

    https://mysite.com/login/ or
    https://mysite.com/register/

IMN logo majestic logo threadwatch logo seochat tools logo