【发布时间】:2014-12-11 16:32:49
【问题描述】:
我想在我的网站用户点击没有尾部斜杠的 REST 路径时重定向他们。
例子。
http://mywebsite.my/it/products/brand/name => http://mywebsite.my/it/products/brand/name/
http://mywebsite.my/it/products => http://mywebsite.my/it/products/
http://mywebsite.my => http://mywebsite.my/
http://mywebsite.my/it/products/brand/name/code.html => ???
好吧,我不希望最后一个被重写,我不希望 URL 以 .html 结尾时的尾部斜杠。
我正在使用 IIS7 的 URL 重写模块,这是我的“斜线规则”。
<rule name="SLASHFINALE" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>
换句话说,如果输入的 url 与该正则表达式匹配(所有 不 以斜杠结尾),我会重写相同的 URL 添加尾部斜杠。
所以我的规则将是相同的,但添加了一点点:重写所有 URL,除了那些(已经)带有斜杠或以 ".html" 结尾的 URL。。 p>
这是我写的
(.*(?<!html)[^\/])$
但我不明白为什么它不起作用。
【问题讨论】:
-
您的lookbehind(如果支持)不在好位置,请将其放在模式的末尾并添加点(转义)以避免与例如
phtml混淆 -
(.*[^/](?
-
在这种情况下,请改用前瞻,但就像这样:
(?!.*\.html$).*[^/]$ -
我正在尝试,确实可以,但它总是匹配:(当我有 .html 时,捕获组总是 html)
-
(?!.*\.html$)(.*[^/])$抓包组我还没写。
标签: regex iis regex-lookarounds url-rewrite-module