【问题标题】:IIS URL Rewrite based on root url基于根 url 的 IIS URL 重写
【发布时间】:2011-12-25 16:05:14
【问题描述】:

我正在尝试重写一个网址:

www.mydomain.com/urlfolder/filename.aspx

指向

www.mydomain.com/urlfolder/newfile.aspx

我的网络配置如下:

    <rule name="PageRedirection" enabled="true" stopProcessing="true">
      <match url="urlfolder/filename.aspx" />
      <action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
    </rule>

问题在于,它正在捕获诸如以下的 url:

www.mydomain.com/subdirectory/urlfolder/filename.aspx

我尝试将我的网址更改为 但是 ^ 没有用。似乎 ~/ 也不能指定根目录。

如果不输入绝对路径,我将如何从根目录指定此 url。

我也有:

testsite.mydomain.com/

我希望部署在那里的 SAME web.config 能够正常工作。 谢谢!

【问题讨论】:

    标签: iis-7 web-config rewrite relative-path url-rewriting


    【解决方案1】:

    我知道这个问题已经有将近 4 年的历史了,所以你可能早就不再需要答案了。

    ^ 应该可以工作:

    <rule name="PageRedirection" enabled="true" stopProcessing="true">
        <match url="^urlfolder/filename.aspx" />
        <action type="Redirect" url="urlfolder/newfile.aspx" redirectType="Permanent" />
    </rule>
    

    ^ 指定路径字符串的开头,因此您在^ 之后放置的任何内容都只会匹配出现在站点根目录和斜杠之后的内容。

    例如

    www.example.com/urlfolder/filename.aspx

    会匹配。

    www.example.com/subdirectory/urlfolder/filename.aspx

    不匹配。

    我能想到它为什么会匹配 /subdirectory/urlfolder/filename.aspx 的唯一原因是你的 web.config 有一个副本 在 /subdirectory/ 中,www.example.com/subdirectory/ 被视为根目录。

    如果是这种情况,并且您的 web.config 被复制到根目录以外的位置,另一种选择是将规则添加到 %windir%\system32\inetsrv\config\ApplicationHost.config 而不是 web.config。该文件包含适用于您机器上所有 IIS 的所有设置,而不仅仅是特定站点。根据我的经验,Visual Studio 会拒绝打开这个文件。但是,您可以在不同的编辑器中编辑文件,或通过 IIS GUI 设置重写规则。

    在 GUI 中,在文件树的顶层,此级别可用的工具包括 URL 重写实用程序,其中设置的规则写入 ApplicationHost.config。

    希望对你有帮助

    【讨论】:

    • 这为我节省了很多时间!谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-01-16
    • 2012-09-14
    • 2010-11-10
    • 1970-01-01
    • 2016-06-27
    • 2013-06-16
    相关资源
    最近更新 更多