negation (!) not working in RewriteCond pattern

Topics: User Forum
Sep 18, 2006 at 6:00 PM
The negation operator (!) does not appear to function correctly with the ReWriteCond pattern. For example:

rewritecond %{HTTP_HOST} !^www\.domain\.com$

Should match (return true) for every possible %{HTTP_HOST} except "www.domain.com" but this is not the case. I've tried this many different ways with no effect.

Any suggestions?

Thanks.
Coordinator
Sep 25, 2006 at 10:46 PM
John, yes, here's a suggestion.

you said,
The negation operator (!) does not appear to function correctly with the ReWriteCond pattern.

you should try using a negative lookahead in the Regular Expression matching the host. like so:

RewriteCond %{HTTP_HOST} ^(?!www\.myhost\.org).*$ I
RewriteRule ^.*$ /redirected.htm U,L


What this says is, if the server variable called HTTP_HOST DOESNOT MATCH www.myhost.org, then apply the RewriteRule. The rule itself, as you can see, rewrites in every case.

The reason your example did not work:

The ^ is a special character when placed at the beginning of the RE. It matches "beginning of line." By placing the negation ahead of that special character, the ^ returns to its normal meaning, which is "regular character."

Anyway, regular expressions are a complex topic. I hope this helps. I've added an example to the IIRF package for this particular case. It will show up in future downloads.

Good luck.