.htaccess 重写冲突规则

.htaccess rewrite conflicting rules

我遇到了一些我认为很简单的 htaccess 规则的问题。我有一些不错的 SEO 友好 URL 重写如下

RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=&%{QUERY_STRING} [L]

这一切都很好,我想保留它。我还希望重写一些旧页面,Google WMT 将其报告为 404 到新的等效页面,为此我想使用:

Redirect 301 /about_us http://example.com/about-us

我遇到的问题是浏览器指向的 URL 是:

http://example.com/about-us?ref=about_us

about_us 是旧的 link,about-us 是正确的 link。如果 htaccess 重定向到 example.com/about-us 那么其他 SEO 友好的重写规则将拾取它并显示页面但是额外的 ?ref= 参数会混淆它。我猜这两条规则在一定程度上是冲突的,但是有没有办法让这两条规则一起工作,例如没有额外的 ?ref= 参数重定向?我对 htaccess 的了解至少可以说是基本的,所以我有点卡在这个上面。

提前致谢

RedirectRedirectMatch 是 mod_alias 的一部分,而重写规则是 mod_rewrite 的一部分。您 运行 遇到的问题是当您将两者混合时,两个 模块都会影响同一个请求,因此当您只想要一个时会发生两件事。在这种情况下,您需要坚持使用 mod_rewrite 并改用它:

RewriteEngine On

RewriteRule ^about_us /about-us [L,R=301]

RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=&%{QUERY_STRING} [L]

请注意,重定向规则 在路由到 index.php 的规则 之前