ISAPI rewriting - redirect to www

Topics: User Forum
Nov 4, 2010 at 1:12 PM

Hi all

We're a small web host running several W2K3 servers (IIS6) and want to be able to assist our web developers by automatically adding SEO friendly www redirects for any domain where the 'www' subdomain/CNAME isn't used within the address bar of a browser.

for example, browser redirects users to even if is entered AND child page redirects: to

This redirect can be seen on and any child page.

I've trawled the web in search of help and found this:

RewriteCond %{HTTP_HOST} ^domain\.TLD [I]
RedirectRule ^/(.*) http://www.domain.TLD/$1 [R=301]

Is it as simple as that, two lines for every domain, and where does this code sit, in 'IirfGlobal.ini'?

Your pointers would be very much welcomed.





Nov 5, 2010 at 11:15 AM

Please let me know if I've used the wrong forum, sorry if I have.

Looking forward to a little assistance.


Nov 5, 2010 at 8:49 PM
Edited Nov 5, 2010 at 8:55 PM

Looks correct, except I would add a line-terminator to the pattern in the RewriteCond.

Like so:

RewriteCond %{HTTP_HOST} ^domain\.TLD$ [I]
RedirectRule ^/(.*) http://www.domain.TLD/$1 [R=301]

Probably if you have multiple domains you will want to provide options in that RewriteCond pattern.  This means you have 2 lines for N domains, rather than 2 lines for each domain.  Like so:

# If hostname is domain1.TLD or or, then redirect to the www. hostname
RewriteCond %{HTTP_HOST} ^(domain1\.TLD||$ [I]
RedirectRule ^/(.*) http://www.%{HTTP_HOST}/$1 [R=301]

If you really want to use www on all hostnames, everywhere, then you can use a wildcard, and a "negative lookahead", like this:

# any time the hostname does not begin with www, redirect (301) to a hostname that begins with www
RewriteCond %{HTTP_HOST} ^(?!www)[^\.]+ [I]
RedirectRule ^/(.*) http://www.%{HTTP_HOST}/$1 [R=301]

Good luck.

Nov 8, 2010 at 2:12 PM
Edited Nov 8, 2010 at 2:22 PM

Thanks Cheeso, that's very much appreciated.

Just to confirm, does the code sit in 'IirfGlobal.ini'? By the way it acts globally I suspect it must, and thus affects every domain in IIS rather than having to create individual Iirf.ini for each domain..  That would be perfect.

And what's the effect on sub domains?  For example will this rule attempt to rewrite: to - that wouldn't be so good...




Nov 9, 2010 at 11:52 AM

Ah, I forgot to answer that part.  As the IIRF documentation states, IIrfGlobal.ini gets the global settings.  The rewrite rules go in IIRF.ini.

The IIRF.ini must be placed in the physical directory that corresponds to the IIS vdir or application.  The rules then apply to the vdir or application.  If you have additional vdirs or applications, then you need additional IIRF.ini files.

Running multiple Domains - by which I guess you mean hostnames - is an orthogonal issue.  I mean it is independent of the vdir/application thing I described above.  The issue is not what hostname is used to get to IIS.  The interesting bit for IIRF is, what vdir or application is activated by the request. What I mean is, I could have a single IIS server with multiple hostnames registered; it's possible that surfing to and surfing to might actually result in activating the same vdir within the same IIS Server.

So the domain you use is not really relevant to IIS configuration.  Often people have multiple distinct domains configured to run in multiple distinct vdirs.  In that case, once again it is the vdir - or specifically the IIRf.ini file within the physical directory that corresponds to the virtual directory (or application) - that determines the IIRF configuration.  The domain is irrelevant, for IIRF.

You also asked "will the rule apply to subdomains?"  It's hard to give a single answer since I offered 3 different versions of the rule.  The final version says, in english, "if the hostname does not begin with www, then redirect to a hostname with www prepended."  That final version of the rule does not distinguish between hostnames that have 2 segments ( versus three segments ( versus any other number.  So the answer is YES, it applies to subdomains.  The prior version of the rule does not, obviously.  It matches only a specific set of hostnames.

I suspect you now have a new set of questions bubbling up in your head.  To answer those, I suggest that you read the IIRF documentation on regular expressions, configuration, and testing.  You'll do well to get the basics there.

Nov 9, 2010 at 1:34 PM

  Hi Cheeso

I appreciate your time and effort to assist and you'll be pleased to know that I don't have any further bubbles ;-)

It's clear from your excellent explanation that as we host individual domains that an iirf.ini is required in each web root folder, therefore there is no such option as a single fully global solution.

Again, thanks for taking the time to assist and IIRF maybe something we can use in the future, or perhaps now should we use scripting to auto generate the hundreds of IIRF.ini files we need.


Nov 9, 2010 at 4:03 PM

Ah - well, if you construct the IIRF.ini file carefully, you may need only one, and you can copy it into the hundreds of directories you have (I guess, one for each site or toplevel vdir).

Whether this is possible depends on your exact requirements.



Nov 11, 2010 at 10:50 AM

Uhm, I take your point:

RewriteCond %{HTTP_HOST} ^(?!www)[^\.]+ [I]
RedirectRule ^/(.*) http://www.%{HTTP_HOST}/$1 [R=301]

could be placed in an iirf.ini and copied to each directory, but my concern is that of subdomains being prepended by www.

Had I experience/knowledge of IIRF syntax then I'm sure it's possible, but the risk of making error and effecting hundreds of domains out ways the requirement in this case.

Again, many thanks

Nov 11, 2010 at 11:49 AM

Well if you want to prepend www only when 2-part hostnames are used, then you could handle that with

RewriteCond %{HTTP_HOST} ^(?!www)[^\.]+\.[^\.]+$ [I]

You'll need to be careful about requests that use the IP address in place of a hostname.  In that case I believe HTTP_HOST will be empty.

Nov 11, 2010 at 3:42 PM


You're a star for assisting, but I guess I'm making a hash of explaining my requirements.

It's actually the opporsite..

I want to prepend ONLY when is used in the browser, NOT when any level of subdomain is used, i.e. or etc...

Good point about IP address hostnames, hadn't thought of that..

Again, many thanks

Nov 11, 2010 at 6:10 PM
Edited Nov 17, 2010 at 2:36 PM
LeaUK wrote:

I want to prepend ONLY when is used in the browser, NOT when any level of subdomain is used, i.e. or etc...

 Yes, that's what I meant when I said "prepend www only when 2-part hostnames are used."   I meant to say, if the request uses a 2-part hostname, for example, , then that RewriteCond will return true.  In that case you want the RedirectRule as I originally suggested, adding www to the hostname.  The set of condition and rule is:

RewriteCond %{HTTP_HOST} ^(?!www)[^\.]+\.[^\.]+$ [I]
RedirectRule ^/(.*) http://www.%{HTTP_HOST}/$1 [R=301]

Does that make sense?

If your user requests a URL with a 3-part name, such as, then no www gets prepended.

Let me break down the regex in the RewriteCond here, just to clarify. The regex begins with ^  . This is a beginning-of-line assertion.  It says, "only allow this pattern to match at the beginning of the line (or input)".  That is in there because otherwise the regex can be matched at any point in the subject string.  (And I'm sure you're aware that the subject string is the thing we're applying the regex to, which in this case is whatever the server variable HTTP_HOST holds.  Mm-kay, still with me?)  

Immediately following the ^ is (?!www) , which says "anything that does not begin with www".  That ?! prefix signals a negative-lookahead assertion in regex-speak.  In English it just means "only match if the next bit in the subject string doesn't match what immediately follows ?!"  . And what follows ?! is www, so .... that assertion matches, or returns TRUE, only when the next three characters are not www .  The ?! is a zero-width assertion, meaning it does not advance the "cursor" in the subject string.  So as far as the regex engine is concerned, it's still looking at the beginning-of-line.  ok.

Ok, Next,  comes a range with a quantifier.  the square brackets denote a range, and the + following the [...] are a quantifier.  The range is actually a negated range, because the first char is ^ . (Hmm, as I write this out I see that this is actually getting pretty complicated, but keep with me here.)  When you see [^.....], where .... is replaced by a set of characters, that regex matches "any character NOT in that set".  In this particular example there is just a single character in the set, and it is the dot.  Because the dot is meaningful to regex, it needs to be escaped - hence backslash-dot, or \.  OK, so [^\.] matches anything that is not a dot, and it is followed by a + quantifier, which matches 1 or more of the preceding thing.  So, [^\.]+ in English, matches "a series of 1 or more of any characters that are not dot".  Got it?  This is intended to match a segment in a hostname.  In "" it would match "domain". 

Next in the regex is \.  , which matches a dot.  Again we have to escape the dot. 

Next is another [^\.]+ , which matches another segment in the hostname. 

Next is $, which matches only at the end-of-line (or end-of-string) .  

Therefore, the entire regex in the RewriteCond matches ONLY a string with exactly 2 segments, separated by a dot. matches, but does not.  Therefore the RewriteCond itself returns TRUE only when a 2-segment hostname is in use, which means the associated RedirectRule applies only when a 2-segment hostname is in use.

Does this help? I know regex is complicated. Take it slow, you'll get it.



Nov 17, 2010 at 10:50 AM

Hi Cheeso

Blimey!  What can I say, apart from thanks.  I'll be honest and say I haven't had chance to digest as yet but just wanted to let you know I've taken a look and again to say thanks.

I'll be back...



Nov 17, 2010 at 2:39 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Nov 17, 2010 at 4:09 PM

Blimey is right!  I didn't realize when I started that it was going to go on that long. I just thought I'd try to provide some insight into how the regex is structured, but the primary thing my long-winded explanation makes clear is that regex is a baroque, inscrutable language.  :(

I'm not sure how I learned all that - it just sort of came out as I was typing.  For those who don't already know it, I don't know what to tell you.  Abandon hope?

No seriously, you can work it through.