regex - IIS 重写规则 - 忽略 localhost

标签 regex iis isapi-rewrite

我有以下规则,可以很好地将我的 www 请求重定向到根目录。

但是我似乎无法为 localhost 关闭它。这是我现在拥有的:

    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://example.com/{R:1}" />
    </rule>

我尝试了很多事情,包括:
    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://example.com/{R:1}" />
    </rule>

你能帮忙吗?正则表达式是我的弱点

最佳答案

如何使用条件仅匹配以 www. 开头的请求?而不是在您不想应用规则时尝试否定?这避免了对 localhost 取反的需要。因为localhost从不匹配条件:

<rule name="Strip WWW" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)" />
    </conditions>
    <action type="Redirect" url="https://{C:1}/{URL}" />
</rule>

但是,您尝试的规则示例(您的第二个代码块)也适用于我在 Windows 10 VM 上使用 IIS 进行测试。我可以浏览到localhost没有重定向。也许这里还有另一个问题。

关于regex - IIS 重写规则 - 忽略 localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50675414/

相关文章:

C# 正则表达式组麻烦

iis - 使用 IIS 将子目录的 URL 重写到不同的域

regex - 替换字符串中每第 n 个字符实例

python - 替换正则表达式匹配中的命名组

regex - 从识别命令解析图像大小

asp.net - IIS 10 Express 性能问题

.net - %2b 导致 IIS Hosted Angular 返回 404

c# - ASP.NET MVC4 + Razor on Mono + EF 6 DateTime 崩溃

debugging - isapi 重写调试或测试工具

c++ - IIS 6 Isapi 过滤器 - 将请求转发或重定向到其他域