我见过一千个这样的例子:
<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 旗舰版)。
最佳答案
你可以尝试匹配这个:
"^([^/]+)/?$"
并替换为:
{R:1}.html
我不确定,但我认为 IIS 与前导/不匹配,所以这可能是导致重写失败的原因。
关于http - IIS 7 友好 URL 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19142453/