【问题标题】:IIS 7 Friendly URL rewriteIIS 7 友好 URL 重写
【发布时间】:2013-10-09 03:55:18
【问题描述】:

我已经看到了一千个这样的例子:

<rule name="ex1" enabled="false" stopProcessing="true">
    <match url="^article/([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="article/{R:1}.html" />
</rule>

我可以很容易地开始工作。然而,我想要做的是让 www.mydomain.net/test 重写为 www.mydomain.net/test.html 我试图使用下面的配置。

<rule name="ex2" enabled="true" stopProcessing="true">
    <match url="^/([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/{R:1}.html" />
</rule>

不幸的是,我没有取得任何成功,每次尝试我都会得到一个简单的 404。虽然,当我测试我的正则表达式时,它对于我希望匹配的任何东西都是成功的 - 例如www.mydomain.net/test.

任何人都可以提供任何关于为什么会这样的见解吗?

(使用 Win7、IIS7 Ultimate)。

【问题讨论】:

  • 不确定第一个规则对第二个没有什么影响?你知道当你点击www.mydomain.net/test 时,只有路径部分(这里是test)与你定义的模式匹配吗?
  • 嗯,第一条规则有效,因为它将 www.mydomain.net/article/test 重写为 www.mydomain.net/article/test.html 这是正确的。第二条规则,考虑到应用于正则表达式的更改,我认为适用于 www.mydomain.net/test => www.mydomain.net/test.html。测试第二个正则表达式对“test”返回真,但对“/test”或“www.mydomain.net/test”不返回真,正如您在正则表达式中从否定 ^[^/] 中很容易看到的那样。
  • 好的,知道了。所以最后,您只想在请求的路径不包含/但可能在末尾时才触发重写规则?
  • 正确!当然 - test/ 应该没问题,但 /test 不行。

标签: http iis url-rewriting seo


【解决方案1】:

您可以尝试在此进行匹配:

"^([^/]+)/?$"

用这个代替:

{R:1}.html

我不确定,但我认为 IIS 与前导 / 不匹配,因此这可能是你重写的绊脚石。

示例:http://regexr.com?36jiq

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 2013-02-02
    • 2013-07-01
    • 2012-12-26
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多