Home > front end >  Remove query string and append value to URL-path
Remove query string and append value to URL-path

Time:06-08

I need to redirect a URL in the following way.

If the user visits:

https://example.com/clientes/?dni=12345

Get redirected to:

https://example.com/clientes/12345

That is, I need to remove this part ?dni= from the URL and redirect to:

https://example.com/clientes/12345

So far I have tried:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^dni=(.*)$
RewriteRule ^clientes$ /%1? [R=301,L]
</IfModule>

It still doesn't work for me, it has no effect.

CodePudding user response:

:
RewriteRule ^clientes$ /%1? [R=301,L]

Your example URL includes a trailing slash, but your RewriteRule pattern does not. This would also redirect to /12345, not /clinetes/12345 as stated.

Try the following instead, at the top of the root .htaccess file (before any existing WordPress directives):

:
RewriteRule ^clientes/$ /$0%1 [QSD,R=301,L]

The $0 backreference contains the URL-path matched by the RewriteRule pattern, ie. clientes/.

The QSD flag (Apache 2.4) discards the original query string. (This is preferred to appending an empty query string on the substitution.)

  • Related