0

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

0

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]
2
  • It replaced the URL well but now I am not able to get these parameter values via $_GET or $_REQUEST Commented 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 loop Commented Jun 2, 2020 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.