使用 mod_rewrite 重写查询字符串 [英] Rewriting query string using mod_rewrite
问题描述
在我的 MVC 应用程序中,我使用 uri 路由器来确定要使用的控制器和操作,并从 uri 中检测 GET 参数.我已经编写了它,以便它接受这两种形式:
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 将 ?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.
推荐答案
如果你真的想将 /controller/action?param1Name=param1Value
形式的请求重定向到 /controller/action/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]
但如果你想反方向:
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)(/.*) $1$4?$2=$3 [QSA]
这篇关于使用 mod_rewrite 重写查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!