mod_rewrite规则以防止查询字符串

时间:2011-01-02 21:43:58

标签: apache mod-rewrite

好的我正在测试我的个人网络服务器上安装的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%{QUERY_STRING}!^ $
RewriteRule ^ index.php $ index.php? [R = 301] 显然重定向所有查询字符串(但它也重定向/注册这不是我想要的)

Register rewriterule末尾的[L]标志也不会使规则处理停止。

修改 结束了丹尼尔的轻推回答。见下面的答案

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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。

相关问题