Using IIS 7.5 URL Rewrite, how can I redirect all requests not from a particular subdomain to a specific page

iis-7.5rewrite

I'd like to use the URL Rewrite module in IIS 7.5 to redirect all requests not from a particular sub-domain (x.domain.com) to a specific folder/file.

For example, these should work as-is:

x.domain.com
x.domain.com/asdf

While anything like these:

y.domain.com
y.domain.com/asdf
domain.com
domain.com/asdf

Should redirect to a specific page like this (exact URL, not dependent upon sub-domain used):

domain.com/a

Unfortunately I can't get the rule configured correctly, as while it matches correctly, most of the time, when it does it just results in a redirect loop. (I know I should put the faulty rules I have setup now, but they're not even consistently resulting in a redirect loop.)

Setting up another Web site in IIS that matches www.domain.com and domain.com is the easy solution, but I'd much rather have one Web site that handles it all and redirects.

What's the correct setup to get this behavior (either using the UI or adding it to the Web.config directly).

Thanks!

Best Answer

Since you haven't posted your existing rule (the one that results in redirect looping), I can't tell you why it doesn't work. I can tell you what should work though:

<rule name="Rewrite all but one subdomain" stopProcessing="true">
  <match url="domain.com" />
  <conditions logicalGrouping="matchAll">
    <add input="{HTTP_HOST}" negate="true" pattern="^x.domain\.com$" />
    <add input="{HTTP_HOST}" negate="true" pattern="^domain\.com$" />
  </conditions>
  <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" />
</rule>

<rule name="Rewrite domain requests" stopProcessing="true">
  <match url="domain.com" />
  <conditions logicalGrouping="matchAll">
    <add input="{HTTP_HOST}" pattern="^domain\.com$" />
    <add input="{PATH_INFO}" pattern="^/a/$" negate="true" />
  </conditions>
  <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" />
</rule>

Here we have to rules, one to rewrite all requests where the url matches domain.com except for host headers exactly matching x.domain.com and domain.com (to avoid looping).

The second rule matches requests for exactly domain.com and to any other place than /a/ and redirects them if necessary