Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

apache - Rewriting query string using mod_rewrite

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...