通过查询字符串使用重写规则
如果 QUERY_STRING
与将使用该规则的内容匹配,我该如何做到这一点?
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/?$ index.php?uri=$1 [L,QSA]
RewriteCond %{QUERY_STRING} uri=admin
ReqriteRule ^admin\?(.*)/?$ index.php?uri=admin&q=$1 [L,QSA]
例如。 http://localhost/admin?poll/new
在 ?
之后应该是参数 q
,查询将是 uri=admin&q=poll/new
关于我如何可以的任何想法做这个吗?
谢谢。
How can I make that if the QUERY_STRING
matches something it would use that rule?
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/?$ index.php?uri=$1 [L,QSA]
RewriteCond %{QUERY_STRING} uri=admin
ReqriteRule ^admin\?(.*)/?$ index.php?uri=admin&q=$1 [L,QSA]
Eg. http://localhost/admin?poll/new
After the ?
should be the paramater q
, the the query would be uri=admin&q=poll/new
Any idea on how I could do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您的问题恰好比我给您的链接更简单,因为您不希望对查询字符串内容进行任何分析。
如果您使用这一行:
其中 QSA 表示将查询字符串附加到结果中。您将获得一个内部重定向到:
这是不行的,这是因为您使用参数 (
admin?poll/new
) 的方式不是标准方式。所以看来我们需要捕获查询字符串内容并将其手动放在 rewriteRule 上。这应该可以工作(如果您只需要 /admin url):其中 %1 是 RewriteCond 中的第一个括号匹配:
(.*)
,表示查询字符串中的所有内容,查询字符串是问号后面的任何内容。所以实际上这允许admin?poll/new
也允许admin?poll/new&foo=toto
,给出index.php?uri=admin&q=民意调查/新&foo=bar
Well, it happens that your problem is more simple than the link I gave you as you do not want any analysis on the query string content.
If you use this single line:
Where QSA mean append the query string to the result. You will obtain an internal redirection to:
Which is not OK, this is because the way you use argument (
admin?poll/new
) is not the standard way. So it seems we'll need to capture the query string content and put it by hand on the rewriteRule. This should work (if you need it only for /admin url):Where %1 is the first parenthesis match in the RewriteCond :
(.*)
, meaning everything in the query string, the query string being anything after the question mark. So in fact this allowsadmin?poll/new
but alsoadmin?poll/new&foo=toto
, givingindex.php?uri=admin&q=poll/new&foo=bar