http to https for main but not sub folder

Topics: Developer Forum, User Forum
Jun 14, 2009 at 6:18 AM
Edited Jun 14, 2009 at 6:25 AM

Im trying to set up a rule to force both www and https for the main site but not to the blog subdomain and am having a tough time getting it to work. Here's what I'm using right now:

RewriteCond %{HTTPS}    (?:off).* [OR]
RewriteCond %{HTTP_HOST}    ^(?!*$
RewriteRule ^(.*)$$1 [R=301,L]

The rule above works, the rule below does not:

RewriteCond %{HTTPS}    (?:off).* [OR]
RewriteCond %{HTTP_HOST}    ^(?!*$
RewriteCond %{REQUEST_URI}    ^(?!/blog).*$
RewriteRule ^(.*)$$1 [R=301,L]


This might be wrong in its entirety, if anyone can make any suggestion please do. Any help will be appreciated.


Jun 16, 2009 at 9:53 AM

I've got a feeling that REQUEST_URI is not supported as it is in mod_rewrite, so you need to use the HTTP_X_REWRITE_URL Server Variable. (I think).

Also, don't you need to be careful about using $1 on conditional RewriteCond statements? There is a section on exactly this kind of thing in the readme. The example given in there looks very similar.

Best of luck.

Jun 16, 2009 at 3:39 PM

Thanks for the reply shonk.

I believe you are correct, REQUEST_URI is not supported so instead I used PATH_INFO and match it to (/blog/.) which seems to match fine. The rule however still does not work. It actually renders the main site inaccessible.

So here's what I think, three base rules are in order:

1. redirect non www to www (regardless if blog or not)

2. if url is not blog redirect to https

3. if url is blog redirect http

THEN - All other blog related rewrites

Any help on creating those three rules would be appreciated :)

And regarding the $1, Im certainly no expert so it probably is a general rule that can(should) be made more descriptive. Any suggestions will be welcome.

Jun 16, 2009 at 7:21 PM
Edited Jun 16, 2009 at 7:54 PM

Did some reading/testing and figured it out. Here's how I got it to work:

RewriteCond %{URL}    ^(?!/blog/.*)
RewriteCond %{HTTPS}    (?:off).* [OR]
RewriteCond %{HTTP_HOST}    ^(?!*$
RewriteRule ^(.*)$$1 [R=301]

So now only non-blog urls get redirected to https. The blog remains neutral.

Jun 17, 2009 at 9:06 AM

Well done, and thanks for sharing what you have figured out.