出于SEO
目的,我的web.config
文件中有这部分代码。它允许我将所有 URL 重定向到我网站 URL 的 www 格式(例如,它将 abc.com
重定向到 www.abc.com
)
<rule name="Redirect to www" stopProcessing="true" >
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true"/>
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false"/>
</rule>
现在我的问题是它还将我网站的 IP 重定向到 www 格式(我的意思是它将 123.123.123.123
重定向到 www.123.123.123.123
)
如何从该操作中排除我网站的 IP?提前致谢。
最佳答案
只需添加另一个否定规则来测试 REMOTE_ADDR 的条件。
<rule name="Redirect to www" stopProcessing="true" >
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true"/>
<add input="{REMOTE_ADDR}" pattern="123\.123\.123\.123" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false"/>
</rule>
关于asp.net - 在 web.config 中重定向到 www 时忽略 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33846416/