apache - 重定向不适用于 htaccess 中的 mod_rewrite

标签 apache .htaccess mod-rewrite redirect

我需要重定向一些具有查询字符串的 URI,例如:

/pages/foo.bar?pageId=123456http://some.site/spam/egg/

/pages/foo.bar?pageId=45678http://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/

相关文章:

Apache-ssl 与 mod_ssl

php - Laravel 身份验证 session cookie 未发送

php - .htaccess 拒绝除允许 javascript 访问之外的所有访问

.htaccess - 如何使用 php 在我的目录中创建友好的 url

apache - 301重定向的必要性

.htaccess - 301 将所有内容重定向到新网站根目录/整个网站(包括子页面)?永久搬家

java - 如何在 Mac OS X 上安装 mod_jk

apache - 当 default virtualhost 不可用时,没有可用的虚拟主机

php - 显示不带扩展名的正确 URL,如果 URL 不正确则重定向

apache - 使用 htaccess 将用户从 http 重定向到 https