如何使用.htaccess将指定URL重定向至/categories路径
解决方法
你的问题出在两个核心点上:
Redirect属于mod_alias模块,和你正在使用的mod_rewrite规则混用会导致执行顺序冲突,重定向规则可能被后面的Rewrite规则覆盖。Redirect无法匹配URL中的查询字符串(?后面的部分),所以第二条针对/index.php?route=common/home的规则根本不会触发。
你需要用mod_rewrite的规则来实现这两个重定向,并且要把重定向规则放在现有Rewrite规则的最前面(保证优先执行)。
完整的.htaccess配置示例
RewriteBase / # 重定向根路径 / 到 /categories RewriteRule ^$ /categories [R=301,L] # 重定向 /index.php?route=common/home 到 /categories RewriteCond %{QUERY_STRING} ^route=common/home$ RewriteRule ^index\.php$ /categories? [R=301,L] # 原有Rewrite规则 RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L] RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L] RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
关键说明
R=301表示永久重定向,适合正式环境;如果还在测试阶段,可以改成R=302临时重定向,避免浏览器缓存重定向规则。- 第二条规则中的
?是为了清除原URL的查询字符串,确保重定向后的URL是干净的/categories而不是/categories?route=common/home。 L标记表示这条规则执行后就停止处理后续规则,避免冲突。
内容的提问来源于stack exchange,提问作者Marvin Klein
相关产品推荐
相关产品推荐

