web.config下旧域名分类首页301重定向失效求助
URL重定向规则优化与新增建议
一、修正现有规则的条件错误
现有规则中的<conditions>配置存在逻辑问题:{HTTP_HOST}仅返回域名(如olddomain.com),不包含协议和路径,因此pattern="https://olddomain.com/oldarticlecategory"永远无法匹配,加上negate="true"会导致规则对所有域名的oldarticlecategory/(.*)路径生效,不符合仅针对旧域名重定向的需求。
修正后的现有规则:
<rule name="redirect personalities articles" enabled="true"> <match url="^oldarticlecategory/(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^olddomain.com$" /> </conditions> <action type="Redirect" url="https://newdomain.com/newurlcategory/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule>
二、新增旧分类根路径重定向规则
现有规则仅能匹配旧分类下带子路径的文章URL,无法处理旧分类的根URL(https://olddomain.com/oldarticlecategory/或https://olddomain.com/oldarticlecategory)的重定向需求。需新增以下规则,并放置在修正后的现有规则上方(规则按从上到下顺序匹配,优先处理根路径避免冲突):
<rule name="redirect personalities root" enabled="true"> <match url="^oldarticlecategory/?$" /> <conditions> <add input="{HTTP_HOST}" pattern="^olddomain.com$" /> </conditions> <action type="Redirect" url="https://newdomain.com/newurlcategory/" appendQueryString="true" redirectType="Permanent" /> </rule>
三、关键规则说明
- 根路径规则的
match url="^oldarticlecategory/?$":^和$确保完全匹配根路径,/?兼容带或不带结尾斜杠的访问请求。 - 两个规则均使用
redirectType="Permanent"(301重定向),利于搜索引擎收录优化。 appendQueryString="true"会保留原URL的查询参数,若不需要可设为false。
内容的提问来源于stack exchange,提问作者Anup Dhal
相关产品推荐
相关产品推荐

