首页 文章

mod_rewrite规则以防止查询字符串

提问于
浏览
4

好吧,我正在测试我的个人网络服务器上安装的cms(joomla),然后再投入使用 . 我希望能够阻止查询字符串的使用,或者更多的是防止用户在查询字符串上输入内容(更改为articleid等),但仍允许内部重定向使用查询字符串 .

示例阻止某人作为网址进入
http://www.doamin.com/index.php?option=com_user&view=register
显示错误页面或重定向到index.php没有查询字符串

但仍然允许重写规则
RewriteRule ^注册$ index.php?option = com_user&view = register

RewriteCond%!^ $
RewriteRule ^ index.php $ index.php? [R = 301]显然重定向所有查询字符串(但它也重定向/注册,这不是我想要的)

寄存器重写结束时的[L]标志也不会使规则处理停止 .

EDIT: 结束了丹尼尔的轻推回答这个问题 . 见下面的答案

2 回答

  • 4

    mod_rewrite documentation说:

    如果要删除现有查询字符串,请仅使用问号结束替换字符串 .

    虽然在这句话中没有提到,重写规则不能使用 QSA 标志 .

    至于仍然允许重写规则:

    RewriteRule ^Register$ index.php?option=com_user&view=register
    

    您可能希望它出现在重写规则下面以去除查询字符串 . 匹配时, %{ENV:REDIRECT_STATUS} 变量设置为200,mod_rewrite再次运行重写规则 . 如果未使用 %{ENV:REDIRECT_STATUS} 不是200的检查,则剥离查询字符串的重写规则将匹配此第二遍 .

    这会将 index.php (带或不带查询字符串)的所有请求映射到没有查询字符串的 index.php ,但仍然允许 /Register 被视为 /index.php?option=com_user&view=register

    RewriteCond %{ENV:REDIRECT_STATUS} !=200
    RewriteRule ^index.php$ index.php?
    
    RewriteRule ^Register$ index.php?option=com_user&view=register
    

    或者,如果您希望在 index.php 的请求具有查询字符串时重定向到错误页面:

    RewriteCond %{ENV:REDIRECT_STATUS} !=200
    RewriteCond %{QUERY_STRING} !=""
    RewriteRule ^index.php$ error.php? [R,L]
    
    RewriteRule ^Register$ index.php?option=com_user&view=register
    

    但我只想使用 F 标志:

    RewriteCond %{ENV:REDIRECT_STATUS} !=200
    RewriteCond %{QUERY_STRING} !=""
    RewriteRule ^index.php$ - [F,L]
    
    RewriteRule ^Register$ index.php?option=com_user&view=register
    
  • 2

    好吧,虽然Daniels的回答没有完全奏效,但它让我开始走上正轨 .

    最终需要两个部分来做我想要的,其中一部分是使用REDIRECT_STATUS变量

    首先需要

    RewriteCond %{QUERY_STRING} !=""<br>
    RewriteCond %{ENV:REDIRECT_STATUS} 200<Br>
    RewriteRule .* - [L]<br>
    

    .....
    我所有的内部重定向
    喜欢: RewriteRule ^Register$ index.php?option=com_register&view=register [L]
    .....

    最后

    RewriteRule .* - [F,L]
    

    使它成为唯一能够使用的东西是内部重定向定义的URL .

相关问题