0

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!

2 Answers 2

2

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

1
  • Two rules. Duh. And while I actually had to tweak this a bit, this got me exactly where I wanted to be. Thanks! Commented Feb 2, 2012 at 13:14
0

Mathias' answer got me what I needed. The exact rules I had to drop in place are below, and involved just two changes:

Match URL is actually based upon the path, so I had to change that to match anything (or nothing), which is fine since IIS has bindings setup correctly on the site and the conditions will capture it correctly.

The enum for logicalGrouping needs to be MatchAll.

<rule name="Rewrite all but one subdomain" enabled="true" stopProcessing="true">
    <match url=".?" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^x.domain\.com$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^domain\.com$" negate="true" />
    </conditions>
    <action type="Redirect" url="http://domain.com/a/" appendQueryString="false" />
</rule>
<rule name="Rewrite domain requests" stopProcessing="true">
    <match url=".?" />
    <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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .