IIS 7.5 URL Rewrite looping

iis-7.5rewritesubdomain

I have a webapp that is located in /subdir1/subdir2/ I'd like to simplify it for the users by adding a subdomain sub.domain.com but it keeps looping. I'd tried to add additional rules to prevent it but no joy.

So what happens is http://sub.domain.com/subdir1/subdir2/subdir1/subdir2/subdir1/subdir2/subdir1/subdir2/subdir1/subdir2/subdir1/subdir2/

My rule in web.config:

<rule name="subdomain" stopProcessing="true">
                <match url="^(?!sub/)(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="sub.domain.com/subdir1/subdir2/" negate="true" />
                    <add input="{HTTP_HOST}" pattern="sub.domain.com" />
                </conditions>
                <action type="Rewrite" url="subdir1/subdir2/" appendQueryString="true" />
            </rule>

Any ideas?

Edit:
So what i'm really trying to do is make it easier for the users. Right now they have to type www.domain.com/subdir1/subdir2/ to access the login page for the product. What I wanted to do was create a single subdomain that would rewrite to the above link. Rather than typing the long url just go to sub.domain.com and it would redirect or rewrite to the www.domain.com/subdir1/subdir2/ location. I hope that makes my desire a bit more clear.

Thanks!

Best Answer

{HTTP_HOST} represents only the host part of a URI:

http://[HTTP_HOST]/subdir/index.html.

Thus, your first condition is irrelevant (it will never evaluate false).

What you want is {PATH_INFO}:

<add input="{PATH_INFO}" pattern="^/subdir1/subdir2" negate="true" />

Effectively saying: "If this part of the URL: http://sub.domain.com[PATH_INFO] starts with "/subdir1/subdir2", don't rewrite

If you want /additional/path/to/content.html rewritten to /subdir1/subdir2/additional/path/to/content.html, you'll need a back-reference to the match as well:

<action type="Rewrite" url="subdir1/subdir2/{R:0}" appendQueryString="true" />
Related Topic