我有以下规则,可以很好地将我的 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/