关于.htaccess中301重定向顺序引发异常的原因咨询
问题原因分析
这是因为Apache的Redirect指令采用前缀匹配规则,且按配置文件中的顺序从上到下处理规则。
原顺序配置的问题
你最初的配置是:
Redirect 301 /leistungen/werbetechnik/point-of-sale-und-messebedarf/ https://mywebsite.com/werbetechnik-wird-jesto-werbetechnik-gmbh/ Redirect 301 /leistungen/werbetechnik/point-of-sale-und-messebedarf/prospekthalter-prospektstaender/ https://mywebsite.com/werbetechnik-wird-jesto-werbetechnik-gmbh/
当用户访问/leistungen/werbetechnik/point-of-sale-und-messebedarf/prospekthalter-prospektstaender/时:
- 第一条规则的源路径是该URL的前缀,会被优先匹配。
- Apache会将匹配路径之后的部分(
/prospekthalter-prospektstaender/)追加到目标URL末尾,导致最终跳转地址变成https://mywebsite.com/werbetechnik-wird-jesto-werbetechnik-gmbh/prospekthalter-prospektstaender/。 - 第二条规则完全没机会执行,因为请求已经被第一条规则处理了。
调换顺序后正常的逻辑
调换后的配置:
Redirect 301 /leistungen/werbetechnik/point-of-sale-und-messebedarf/prospekthalter-prospektstaender/ https://mywebsite.com/werbetechnik-wird-jesto-werbetechnik-gmbh/ Redirect 301 /leistungen/werbetechnik/point-of-sale-und-messebedarf/ https://mywebsite.com/werbetechnik-wird-jesto-werbetechnik-gmbh/
此时:
- 访问长路径时,第一条规则精准匹配完整路径,直接跳转到目标URL,不会追加任何后缀。
- 访问短路径时,才会触发第二条规则,正常完成跳转。
关键结论
使用Redirect指令配置重定向时,必须把更具体、路径更长的规则放在前面,避免被更宽泛的前缀规则提前拦截处理。
内容的提问来源于stack exchange,提问作者user2439629
相关产品推荐
相关产品推荐

