我可以将所有domain.com-s重定向到www.domain.com上,并且它在服务器上的运行就像一个 super 按钮:
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
但这有一个副作用-当我在测试环境中(例如在localhost上)它也可以工作,并且它总是在生产服务器上重定向。
如何添加仅在服务器上起作用的某些条件?
最佳答案
这可以通过使用第二个条件来请求主机成为domain.com
子域来实现:
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain\.com$" />
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
</rules>
</rewrite>
(请注意,您的状况中缺少
\
,并且redirectType="Permanent"
是可选的)关于iis - 仅在服务器上进行规范重定向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15745366/