Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top