重写使用mod_rewrite查询字符串 [英] Rewriting query string using mod_rewrite
问题描述
在我的MVC应用程序中我用比决定了控制器和动作,要使用的,检测得到的的URI参数的URI路由器。我已经写它,以便它将接受这两种形式:
In my MVC application I use a uri router than determines which controller and action to use and detects GET parameters from the uri. I've written it so that it will accept both these forms:
http://localhost/controller/action/param1Name/param1Value
http://localhost/controller/action?param1Name=param1Value
现在我想要做的是使用mod_rewrite重定向 2 P = V
的形式向 / P / V
形式(推理纯粹是化妆品,GET形式使用?X = Y
形式)。我完全坚持我怎么会做这个但是 - 我有一个想法,我需要使用 $ {QUERY_STRING}
,但我不知道如何
Now what I'd like to do is use mod_rewrite to redirect the ?p=v
form to the /p/v
form (reasoning is purely cosmetic, GET forms use the ?x=y
form). I'm completely stuck with how I'd do this however - I have an idea I need to use ${QUERY_STRING}
but I'm not sure how.
推荐答案
如果你真的想重定向形式要求 /控制器/动作?param1Name = param1Value
到 /控制器/动作/ param1Name / param1Value
,试试这个:
If you really want to redirect requests of the form /controller/action?param1Name=param1Value
to /controller/action/param1Name/param1Value
, try this:
RewriteCond %{THE_REQUEST} ^GET\ /[^/]+/[^/]+\?[^\s]+
RewriteCond %{QUERY_STRING} ^([^=&]+)=([^&]+)&?(.*)
RewriteRule ^[^/]+/[^/]+.* /$0/%1/%2?%3 [N]
RewriteCond %{THE_REQUEST} ^GET\ /[^/]+/[^/]+\?[^\s]+
RewriteRule ^[^/]+/[^/]+.* /$0 [L,R=301]
但是,如果你想相反的方式:
But if you want to opposite way:
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)(/.*) $1$4?$2=$3 [QSA]
这篇关于重写使用mod_rewrite查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!