Link to home
Start Free TrialLog in
Avatar of Luey
LueyFlag for United States of America

asked on

mod_rewrite to https or http

I have a rewrite rule in my htaccess file that sends all traffic to https.  I have a unique situation that requires one page in my site to not be redirected to https.  How can I accomplish this.

This is the rule I am using to send everything to https.
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301,L]

Open in new window


However, I need this rule to go to http instead.
RewriteRule ^/?fb/(.*)$ /social_redirects.php?sign_serial=$1  [QSA,NC,L]

Open in new window

Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Not tested, but you could negate the request URI before the https rule:
RewriteCond %{REQUEST_URI} !^/?fb/(.*)$
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301,L]

Open in new window

HTH,
Dan
Avatar of Luey

ASKER

Dan I tested that and it still redirects to https any more ideas?
Weird. It should of worked.

Anyway, add the full path to the second rule.

RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L]

Open in new window

Avatar of Luey

ASKER

If I put it like this it still does not work.  Still redirects all to https
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301,L] 
RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L] 

Open in new window


If I put it like this it fails in the browser and says this webpage has a redirect loop.
RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L] 
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301,L]

Open in new window

Both rules have an L at the end, meaning they are the last rules used, if true.

So yes, this:
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301,L] 
RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L]

Open in new window

will always match rule number 1, and never even try rule number 2

But reversing the order should produce correct results: if the page is of the form http://www.bumpum.com/fb/something then the first rule is applied and the next is ignored, if not then the second rule is applied.

Can you please try another browser? Maybe it's a caching issue.
Avatar of Luey

ASKER

I have now tried multiple browsers and multiple computers.  I always get the same results.  

If i put this first it comes back with a browser failure (redirect loop)  Its like the L is not working at all.
RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L]

Open in new window


I also tried it like this and it does not work either.  I removed the L thinking it might move to the next rule but does not work.  I tried every combination I can think of.
RewriteRule ^(.*)$ https://www.bumpum.com/$1 [R=301] 
RewriteRule ^/?fb/(.*)$ http://www.bumpum.com/social_redirects.php?sign_serial=$1  [QSA,NC,L]

Open in new window

Same solution as Dan, but you also need to exempt /social_redirects.php from the https redirect.
In PHP you can detect HTTPS and use header("Location:...") to redirect the browser.  You would want to be mindful of the risk of complex interaction between .htaccess and the PHP redirect.
http://php.net/manual/en/reserved.variables.server.php
I have a very similar problem to this one.

All traffic to a site must be https, so I rewrite all URLS to redirect to https.
BUT I have one URL which comes not from the browser but an embedded activeX control which must go directly to CGI. This does not work.

I'm still fiddling, so I'll keep you posted.
Avatar of Luey

ASKER

@ray - the reason I cannot use that is I am using NFC technology in my application to redirect to a social media ap and if I send it to https first it opens up a browser window and sends them to the site in the browser which is not what I want to do.  If I use http: it sends them to my redirect page then the phone prompts the user to use their social ap to open and that is what I want to do.  So I only need the one page to go to http: and all others to go to https:  It will need to be done in the rewrite.

@Steve - I do not know how to implement what you are saying.  If you could give me an example it would be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luey

ASKER

That sounds like a great solution Ray.  I will implement that and see how well it works out.  Thanks
Good luck with it.  I'm pretty sure it's sound in theory, so if you have any issues implementing it in practice, please post back here and we will be glad to help!