'How to rewrite "something/test?id=test" to "test.php?id=test" in .htaccess

How to rewrite something/test?id=test to test.php?id=test in .htaccess

RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

Nothing works!



Solution 1:[1]

RewriteRule ^v1/games/list?sortToken=(.*)$ /juicy.php?id=$1 [QSA,NC,L]

The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. To match the query string you need to use an additional condition and match against the QUERY_STRING server variable.

How to rewrite something/test?id=test to test.php?id=test

However, the example given in the question is quite different to the rewrite implied by your RewriteRule attempt?

To rewrite this example, you could use something like this:

Options -MultiViews

RewriteEngine On

RewriteRule ^[^/]+/(test)$ $1.php [L]

The above will internally rewrite /<something>/test?id=test to test.php?id=test. The query string is passed through by default, so this matches any query string.

However, to rewrite /v1/games/list?sortToken=<something> to /juicy.php?id=<something> (as implied by your "attempt"), you would need to do something like the following instead:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^sortToken=([^&]*)
RewriteRule ^v1/games/list$ juicy.php?id=%1 [NE,L]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrWhite