IIS7.5 web.config如何匹配带查询字符串的路径实现站点重定向
问题原因
- IIS 原生的
<httpRedirect>模块的通配符仅匹配URL的路径部分,不会识别?后的查询字符串内容,你配置的/index.php?q=zh-hans*这类包含查询参数的规则完全无法命中,所有请求最终都会匹配到最后一条*通配符规则,所以简繁体请求都跳转到了英文页面。
解决方案
使用IIS的URL Rewrite模块实现带查询字符串匹配的重定向,操作如下:
- 为IIS7.5安装URL Rewrite模块
- 清空原有
<httpRedirect>配置,在站点web.config的<system.webServer>节点下添加如下规则:
<rewrite> <rules> <!-- 简体中文重定向 --> <rule name="Redirect Hans To New SC Site" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{QUERY_STRING}" pattern="q=zh-hans.*" /> </conditions> <action type="Redirect" url="https://newsitedomain.com/sc/somename" appendQueryString="false" redirectType="Permanent" /> </rule> <!-- 繁体中文重定向 --> <rule name="Redirect Hant To New TC Site" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{QUERY_STRING}" pattern="q=zh-hant.*" /> </conditions> <action type="Redirect" url="https://newsitedomain.com/tc/somename" appendQueryString="false" redirectType="Permanent" /> </rule> <!-- 剩余所有请求跳英文站 --> <rule name="Redirect All To New EN Site" stopProcessing="true"> <match url=".*" /> <action type="Redirect" url="https://newsitedomain.com/en/somename" appendQueryString="false" redirectType="Permanent" /> </rule> </rules> </rewrite>
上述规则会自动匹配所有带zh-hans/zh-hant前缀的q参数请求,跳转到对应语言的新站首页,其余所有请求跳转英文站,appendQueryString="false"配置会丢弃旧站的查询参数,符合你所有页面都跳对应新站首页的需求,redirectType="Permanent"对应301永久重定向,对搜索引擎更友好。
内容的提问来源于stack exchange,提问作者Cody Ng
相关产品推荐
相关产品推荐

