这是我的.htaccess文件

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteRule subpage\?sid /index.php [R=301,L,NC]

我的请求是 http://example.com/subpage?sid

它一直返回404 Not Found,而不是重定向到index.php

我试过逃避了吗?并将请求更改为 http://example.com/subpage\?sid

我已经尝试了大量的东西来尝试让这个工作,但现在我很难过。

有帮助吗?

解决方案

QUERY_STRING不是URI的一部分。来自 RewriteRule 的文档:

  

注意:查询字符串

     

Pattern 将不会与查询字符串匹配。相反,您必须使用带有%{QUERY_STRING} 变量的RewriteCond。

正如文档所说,你需要使用RewriteCond,例如:

RewriteCond %{QUERY_STRING} (^|&)sid([&=]|$) [NC]
RewriteRule ^/?subpage /index.php [R=301,L,NC]

如果%{QUERY_STRING}确实不是一个选项,请尝试:

# Apache is supposed to use PCRE, but doesn't seem to like "\\s"
RewriteCond %{THE_REQUEST} subpage?sid[^!-~] [NC]
RewriteRule ^/?subpage /index.php [R=301,L,NC]

此外,您在使用它时几乎不需要RewriteBase(“RewriteBase /”)。评论该行并对其进行测试以确定,但您可能会发现可以删除它并且没有任何不良影响。

其他提示

您是否需要检查站点的VirtualHost设置并为相应的目录设置AllowOverride设置?

<Directory /var/www/vhosts/some_host>
     ....
     AllowOverride All
</Directory>

这里有更多关于apache的 AllowOverride

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top