Overwrite query string with mod_rewrite

In my MVC application, I use the uri router, which determines which controller and action to use and detects the GET parameters from the uri. I wrote it so that it takes both of these forms:

http://localhost/controller/action/param1Name/param1Value
http://localhost/controller/action?param1Name=param1Value

Now, what I would like to do is use mod_rewrite to redirect the form ?p=vto the form /p/v(the reasoning is purely cosmetic, GET forms use the form ?x=y). I was completely fixated on how I would do this, but I have an idea, I need to use it ${QUERY_STRING}, but I'm not sure how to do it.

+3
source share
2 answers

/controller/action?param1Name=param1Value /controller/action/param1Name/param1Value, :

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]
+2

, POST , URL-.

- !

+1

Source: https://habr.com/ru/post/1713488/


All Articles