代码之家  ›  专栏  ›  技术社区  ›  Antony Carthy

mod rewrite complete befuzzlement[关闭]

  •  1
  • Antony Carthy  · 技术社区  · 15 年前

    这是我的.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

    我试过很多东西想让它工作,但现在我受不了了。

    2 回复  |  直到 15 年前
        1
  •  4
  •   outis    15 年前

    查询字符串不是URI的一部分。从文档中 RewriteRule :

    注意:查询字符串

    这个 模式 将与查询字符串不匹配。相反,您必须在 %{QUERY_STRING} 变量。

    正如医生所说,您需要使用重写秒,例如:

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

    如果查询字符串确实不是一个选项,请尝试:

    # 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/”)。评论这一行并测试它以确定是否正确,但您可能会发现您可以删除它而不会产生不良影响。

        2
  •  0
  •   Igor Zinov'yev    15 年前

    也许您需要检查站点的虚拟主机设置,并为相应的目录设置allowoverride设置?

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

    以下是关于阿帕奇的更多信息 AllowOverride

    推荐文章