Domain based ssl redirect %HTTP_HOST

Topics: Developer Forum, User Forum
Oct 31, 2008 at 3:50 PM
So i have a series of domains which all point to a single IP address. I want to redirect any domain not equal to to when https equals on. I am having trouble getting a "not equal" expression to work.

I would think something like this should do the trick:
RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} !^www\.xyz\.com$
RewriteRule ^/foo/bar/(.*)$$1   [I,U,R=301]

Does ionics not support the "!" or match if expression isnt true? If so how else can this be aceived. I dont have control over the domains registered and pointing to this site.

Oct 31, 2008 at 6:35 PM
Edited Nov 4, 2008 at 6:39 PM
IIRF does not support the use of an initial "!" to match if an expression isn't true.
IIRF depends on the PCRE library for its regular expression support. What that means is that if PCRE supports a regular expression construct, then IIRF "supports it". Really, IIRF doesn't do anything with the regular expressions except pass them to PCRE.
In any case....
You can use a negative look-ahead to do what you want.
For example (?!pattern)
matches any string that does not begin with pattern. It does not capture.
There are examples of this on this site - go to the home page and click the examples link.
There are also lots of examples of this in the Readme.txt file.
For more on PCRE and the regex syntax it supports, look here:
and scroll about halfway down that document, to the section entitled "PCRE - Perl-compatible regular expressions" (search for the term PCREPATTERN.
That document is fairly exhaustive, and for someone who is not familiar with regular expressions, it can be a little unapproachable. But still it is a good reference. About 75% of the way down, there is a summary of the PCRE RegEx syntax.
You can also play with the testdriver.exe to try out different regular expressions and how they match strings.

Nov 3, 2008 at 4:44 PM
Hi, Thanks for the reply. I tried what you suggested above and it still does not work.

RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} (?!www\.xyz\.com)
RewriteRule ^/foo/bar/(.*)$$1   [I,U,R=301]

This always results to true so it goes into a infinite loop. Any ideas would be appreciated.


Nov 4, 2008 at 6:44 PM
I would need to see the logfile to help diagnose this.

One thing I would try: tighten up the HTTP_HOST condition to specify that the negative lookahead applies at the beginning of the string.
In other words, add a leading ^ to the (?!www\.xyz\.com) .

The updated regex says:  "any string that DOES NOT have at the beginning"

The original regex says "any string that DOES NOT have (at some point)"  but I am not sure how PCRE would interpret "at some point".  Depending on how PCRE works internally, it could always return true.  You can remove the ambiguity by adding the caret.