views:

693

answers:

2

I am trying to create a rewrite rule that accomplishes two things:

  1. Redirect (www.)?domain.com to log.domain.com
  2. Let any other directory or file request on domain.com through without redirection

This not an Apache server (it's LiteSpeed), but supposedly it supports .htaccess files wholly.

This was my shot at the rule:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule .* http://log.domain.com/

What's happening is that if I request a specific file (like dir/index.php) it's let through, but directory requests are still redirected to log.domain.com. I thought the $ of the second RewriteCond would keep this from happening, but that seems to not be the case.

EDIT: Just for archival purposes my intention was not to redirect any nonexistent directories/files to the log.domain.com since users should never have to do that. This made the criteria a lot simpler for the rule, which chaos and gumbo both arrived at with their first rules below. Thanks again!

+2  A: 
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^$ http://log.domain.com/
chaos
This works for letting directories or files through, but it now stopped redirecting (www.)?domain.com to log.domain.com.
VTJustinB
Mmm, yeah, / being a directory and all. Give the revised version a shot...
chaos
REQUEST_FILENAME is never just “/”. (Except you use something like this: “RewriteRule ^ /”)
Gumbo
The revised rule didn't do the trick either--still fails to redirect to log.domain.com.
VTJustinB
Instead of trying to handle the exceptions, would it make more sense to ONLY redirect if the user requests exactly (www.)?domain.com(/)? and nothing else? I'm not sure how to do that, but that seems like it might be an easier approach.
VTJustinB
I thought we were going through all this because OP wanted to also redirect all 404s.
chaos
@VTJustinB: Another try up...
chaos
@chaos that didn't seem to work either, sorry.The purpose of this rewrite is to route all visitors to log.domain.com, which is a custom A record that points to my tumblr.com blog. I still use the webserver for file storage, so I need to be able to supply directories/files and not be redirected.
VTJustinB
Okay, had a little thought. If this one doesn't work, then it's brute force time.
chaos
Haha yeah that one failed to redirect as well. I'm okay with brute force though. =)
VTJustinB
Okay, there's the brute force. Not real different from Gumbo's. One of them's bound to work...
chaos
Oddly enough, just the first rule seems to work for everything. Directories/Files go through, and domain.com requests are routed to log.domain.com
VTJustinB
... oh. I could've sworn you wanted to reroute stuff like www.domain.com/thisdoesnotexist. Well, okay then, I'll just chop out the cruft. :)
chaos
doh no sorry--I don't anticipate needing to do that for 404 purposes since nobody by myself should be using the domain for anything other than getting redirected to my blog. Thanks!
VTJustinB
Glad to be of service. :)
chaos
A: 

Try this:

# skip the next rule if host is not example.com or www.example.com
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S]

# redirect if requested URI does not match a regular file or directory
# or if the requested URI path is just "/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ http://log.example.com/
Gumbo
Thanks Gumbo, I believe your first rule would do the trick as well.
VTJustinB