3

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.

3 Answers 3

5

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/

6
  • 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. Aug 29, 2009 at 0:13
  • 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! :)
    – Thus
    Aug 29, 2009 at 0:19
  • 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. Aug 29, 2009 at 0:26
  • I'm happy to hear that I could help. I edited the syntax of my answer to your corrected version.
    – Thus
    Aug 29, 2009 at 0:30
  • 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)
    – David Z
    Aug 29, 2009 at 2:45
2

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

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

(edited)

1
  • I had to remove the space between the ! and the ^ to get this to work for me, otherwise I was getting "bad flag delimiters".
    – philfreo
    Jan 21, 2010 at 0:32
0

There are examples for apache: https://httpd.apache.org/docs/trunk/rewrite/remapping.html#canonicalhost

You can alternatively accomplish this using the directive: (2.4 and later)

Redirect "/" "http://www.example.com/" Or, for example, to redirect a portion of your site to HTTPS, you might do the following:

Redirect "/admin/" "https://www.example.com/admin/"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .