.htaccess重写规则跳转错误,请求排查问题原因
问题分析与解决方案
问题原因
你的重写规则顺序和模块执行逻辑导致了异常跳转:
- 你先定义了旧域名跳转至新域名的
RewriteRule,所有来自abc-something-old.com的请求都会优先触发这条规则,直接跳转到新域名的对应路径(比如/products/vending.php),并通过[L]标志终止当前规则处理。 - 浏览器跳转后发起的新请求目标是新域名
myglobalsite.com,此时你的redirectMatch规则是针对旧域名路径的,不会再触发,因此最终跳转到了新域名的原路径,而非预期地址。
另外,mod_rewrite(RewriteRule所属模块)的执行优先级高于mod_alias(redirectMatch所属模块),即使调整顺序,redirectMatch也可能被RewriteRule抢先处理。
修正后的.htaccess配置
将特定路径的重定向规则移到最前面,统一使用mod_rewrite规则(避免模块执行顺序冲突):
RewriteEngine On # 先处理特定路径的301重定向 RewriteRule ^products$ https://www.myglobalsite.com/en/product/drinking-cups [R=301,L] RewriteRule ^products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups [R=301,L] RewriteRule ^products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups [R=301,L] # 再处理旧域名整体跳转 RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC] RewriteRule ^(.*)$ https://www.myglobalsite.com/$1 [R=301,L] ## DIRECTORY配置 Options +FollowSymLinks Options All -Indexes DirectoryIndex index.php ErrorDocument 404 https://www.myglobalsite.com/
关键说明
- 规则顺序:特定路径重定向优先于全局域名跳转,确保旧域名下的特定路径请求先被匹配到目标地址,直接跳转后终止处理。
- 使用
R=301明确指定永久重定向(原redirectMatch默认是301),[L]标志确保匹配到规则后不再执行后续规则。 - 统一用
mod_rewrite规则,避免不同模块执行顺序带来的冲突。
内容的提问来源于stack exchange,提问作者Paul T. Rykiel
相关产品推荐
相关产品推荐

