我需要重定向一些具有查询字符串的 URI,例如:
/pages/foo.bar?pageId=123456
到 http://some.site/spam/egg/
/pages/foo.bar?pageId=45678
到 http://another.site/spaming/egging/
我的 htaccess 中有这个:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
但它不起作用,显示 404。我做错了什么?
最佳答案
您需要移动这 2 条规则,即在 RewriteEngine On
行下方的所有其他规则之前移动,因为其他规则可能会覆盖此规则。
(根据您的评论)您的罪魁祸首规则是这条规则:
RewriteRule . index.php [L]
这实际上是将每个请求重写到 index.php
并将 REQUEST_URI
变量的值更改为 /index.php
从而导致这种情况失败:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
关于apache - 重定向不适用于 htaccess 中的 mod_rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34398544/