Search code examples
.htaccessmod-rewrite

Htaccess - RewriteRule, how to keep only the value part of a query string


I'm trying to rewrite an url from:

https://www.website.com/test/test-detail?name=abc

to:

https://www.website.com/test/abc

I can't seem to find a way to keep only the value part of the query string.

This is the code:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=
RewriteRule ^(test)/(test-detail)/$ /test/$3 [R=301,L]

For the input:

https://website.com/test/test-detail/?name=abc

The result is:

https://website.com/test/?name=abc

Link to the example.


Solution

  • You can use this :

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/test/test-detail/
    RewriteCond %{QUERY_STRING} name=(.+)$
    RewriteRule ^(test)/(test-detail)/$ /test/%1?  [R=301,L]
    

    Make sure to clear your browser's cache before you test this.