IIS 7 URL Rewrite Match URL

五迷三道 提交于 2019-12-09 01:42:31

问题


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."



来源:https://stackoverflow.com/questions/10999735/iis-7-url-rewrite-match-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!