【问题标题】:Matching strings not ending with .html匹配不以 .html 结尾的字符串
【发布时间】: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


【解决方案1】:

IIS Javascript 风格的正则表达式解析器不支持条件表达式。

我最终得到了这个:

<match url="(.*[^\.]...[^\/]$)" />

对我来说足够了。

【讨论】:

    猜你喜欢
    • 2014-03-03
    • 2017-04-26
    • 2014-11-26
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多