I want to rewrite some dynamic urls like:
https://www.test.com/my-custom-url.html/?o=price&dir=asc&new=1
to
https://www.test.com/my-custom-url.html/o/price/dir/asc/new/1
Thanks.
1 Answer 1
You can rewrite a query string like this:
Example for one arbitrary parameter:
RewriteCond %{REQUEST_URI} ^/my-custom-url.html.*$
RewriteCond %{QUERY_STRING} ^(.*)=(.*)$
RewriteRule .* /my-custom-url.html/%1/%2? [R=301,NC,L]
Example for 3 arbitrary parameters:
RewriteCond %{REQUEST_URI} ^/my-custom-url.html.*$
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$
RewriteRule .* /my-custom-url.html/%1/%2/%3/%4/%5/%6? [R=301,NC,L]
answered Jun 2, 2020 at 8:57
HelgeB
4,5512 gold badges12 silver badges27 bronze badges
-
It replaced the URL well but now I am not able to get these parameter values via $_GET or $_REQUESTYogita– Yogita2020年06月02日 09:36:55 +00:00Commented Jun 2, 2020 at 9:36
-
OK, that was not part of your question :-) For that you need an additional line, for example this for one parameter:
RewriteRule ^/my-custom-url.html/[a-z]+/[a-zA-Z0-9-]+$ my-custom-url.html?1ドル=2ドル [L]Important: this should not redirect! Put it above the other rule I'm not sure about this, it might end in an infinite loopHelgeB– HelgeB2020年06月02日 10:21:46 +00:00Commented Jun 2, 2020 at 10:21
default