Conditional https redirect to http depending on URL? (Apache)

13,837

Solution 1

Try adding a RewriteCond (Rewrite Condition) before the RewriteRule. I would try writing something like this:

RewriteCond %{REQUEST_URI} !^.*(abc).*$ [NC]

Here is some additional reading if this doesn't help:
- http://www.whoopis.com/howtos/apache-rewrite.html
- http://www.sitepoint.com/article/apache-mod_rewrite-examples/

Solution 2

I'll reproduce the answer I gave you on the Slicehost forums:

RewriteRule !^/abc/ http://%{HTTP_HOST} [L,R=permanent]

(edited)

Share:
13,837

Related videos on Youtube

Joel Marcey
Author by

Joel Marcey

Updated on September 17, 2022

Comments

  • Joel Marcey
    Joel Marcey over 1 year

    Right now I redirect 100% of the time if someone does https://mysite.com

    <VirtualHost *:443>
       ServerAdmin [email protected]
       ServerName mysite.com
       ServerAlias www.mysite.com
    
       RewriteEngine on
       RewriteRule (.*) http://%{HTTP_HOST} [L,R=permanent]
    <VirtualHost>
    

    However, now I want to conditionally redirect. If a user goes to https://mysite.com/abc/, then I want to use https; otherwise redirect.

    How do I do this? I tried reading the docs, but just couldn't find what I needed.

    I am using Apache on Ubuntu Linux.

  • Joel Marcey
    Joel Marcey over 14 years
    Thus, what you offered seems to make sense on the surface, but I get this when reloading Apache RewriteCond: bad argument line '!^%{HTTP_HOST}/abc'. I am trying to read the links you sent me to see if there is any more hints.
  • Mateusz Kowalski
    Mateusz Kowalski over 14 years
    It is probably just my syntax that is wrong. I don't have a machine to test it on myself at the moment. Please post the correct syntax when you figure it out. Good luck! :)
  • Joel Marcey
    Joel Marcey over 14 years
    Well, I tried different syntax that seemed to work --- RewriteCond %{REQUEST_URI} !^.*(abc).*$ [NC] --- Thanks for your help and trying to point me in the right direction.
  • Mateusz Kowalski
    Mateusz Kowalski over 14 years
    I'm happy to hear that I could help. I edited the syntax of my answer to your corrected version.
  • David Z
    David Z over 14 years
    This answer will also use HTTPS for things like https://mysite.com/foo/test/abcdef, which I think is not what you (Joel) wanted. And anyway, it's way overkill to add on a RewriteCond just to test against the request URI. (Not downvoting, though... it does technically work)
  • philfreo
    philfreo over 14 years
    I had to remove the space between the ! and the ^ to get this to work for me, otherwise I was getting "bad flag delimiters".