简体   繁体   English

mod_rewrite规则以防止查询字符串

[英]mod_rewrite rule to prevent query string

Ok i am testing a cms(joomla) installed on my personal webserver before putting it live. 好吧,我正在测试我的个人网络服务器上安装的cms(joomla),然后再投入使用。 And i want to be able to prevent the use of the query string, or more to the point prevent users from entering stuff on the query string (changing like articleid etc), but still allow the internal redirecting to use the query string. 而且我希望能够阻止查询字符串的使用,或者更多的是防止用户在查询字符串上输入内容(更改像articleid等),但仍允许内部重定向使用查询字符串。

Example prevent someone from entering as the url 示例阻止某人作为网址进入
http://www.doamin.com/index.php?option=com_user&view=register http://www.doamin.com/index.php?option=com_user&view=register
display Error page or redirect to index.php without query string 显示错误页面或重定向到index.php没有查询字符串

But still allow the rewrite rule 但仍然允许重写规则
RewriteRule ^Register$ index.php?option=com_user&view=register RewriteRule ^注册$ index.php?option = com_user&view = register

RewriteCond %{QUERY_STRING} !^$ RewriteCond%{QUERY_STRING}!^ $
RewriteRule ^index.php$ index.php? RewriteRule ^ index.php $ index.php? [R=301] obviously redirects all query strings (but it also redirects /Register which isnt what i want) [R = 301]显然重定向所有查询字符串(但它也重定向/注册,这不是我想要的)

The [L] flag on the end of the Register rewriterule doesnt make it stop the rule processing either. 寄存器重写结束时的[L]标志也不会使规则处理停止。

EDIT: Ended up answering this with a nudge from Daniel. 编辑:结束了丹尼尔的推动回答这个问题。 See answer below 见下面的答案

The mod_rewrite documentation says: mod_rewrite文档说:

When you want to erase an existing query string, end the substitution string with just a question mark. 如果要删除现有查询字符串,请仅使用问号结束替换字符串。

And though not mentioned in this sentence, the rewrite rule must not use the QSA flag. 虽然在这句话中没有提到,但重写规则不得使用QSA标志。

As far as still allowing the rewrite rule: 至于仍然允许重写规则:

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

You probably want that to appear below the rewrite rule to strip the query string. 您可能希望它出现在重写规则下面以去除查询字符串。 When it matches, the %{ENV:REDIRECT_STATUS} variable is set to 200 and mod_rewrite runs through the rewrite rules again. 匹配时, %{ENV:REDIRECT_STATUS}变量设置为200,mod_rewrite再次运行重写规则。 The rewrite rule to strip the query string would match on this second pass if a check that %{ENV:REDIRECT_STATUS} is not 200 were not used. 如果未使用%{ENV:REDIRECT_STATUS}不是200的检查,则剥离查询字符串的重写规则将匹配此第二遍。

This will map all requests for index.php (with or without a query string) to index.php without a query string, but still allow /Register to be treated like /index.php?option=com_user&view=register : 这会将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

Or, if you want to redirect to an error page if a request for index.php has a query string: 或者,如果您希望在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

But I would just use the F flag: 但我只想使用F标志:

RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^index.php$ - [F,L]

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

Ok while Daniels answer did not fully work, it got me started on the right track. 好吧,虽然Daniels的回答没有完全奏效,但它让我开始走上正轨。

ended up needing two parts to do what i wanted, part of it was using the REDIRECT_STATUS variable 最终需要两个部分来做我想要的,其中一部分是使用REDIRECT_STATUS变量

first needed 首先需要

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

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

then finally 最后

RewriteRule .* - [F,L]

Makes it so that only thing able to be used are the urls defined by the internal redirects. 使它成为唯一能够使用的东西是内部重定向定义的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM