IIS 7 URL Rewrite Match URL

2019-02-15 22:46发布

问题:

I'm trying to set a canonical default URL in IIS 7 using the URL Rewrite module. I think that I'm misunderstanding how the 'Match URL' field is used. The following doesn't seem to do anything:

    <rewrite>
        <rules>
            <rule name="EnforceDefaultPage">
                <match url="^http://(?:www\.)?mydomain\.com(?:/)?(?:blog\.aspx)?$" />
                <action type="Redirect" url="http://www.mydomain.com/blog" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite> 

I've noticed in a lot of examples that people have added a condition utilizing the HTTP_HOST variable... but how does this relate to the match url? It seems that I should be able to omit any conditions because my regex matches exactly what I want.

回答1:

Ahh, I've finally figured it out. Apparently how 'much' of the URL is available for matching depends on the location of the web.config in the directory hierarchy. Since I was placing the code in the web.config in the web root, it could only match anything after the domain name (i.e. it can match everything after 'blog.com/' in 'http://www.blog.com/').

I found the answer here: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

"Note that the input URL string passed to a distributed rule is always relative to the location of the Web.config file where the rule is defined. For example, if a request is made for http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule is defined in the /content directory, then the rule gets this URL string default.aspx as an input."