.htaccess redirect with attributes

Viewed 52

I am trying to make a .htaccess redirect rule to achieve the following

https://www.example.net/index.php?a=in&u=testUser&api_key=4r98f4r98f&ip_address=127.0.0.1

TO

https://www.example.net/api.php?ip=127.0.0.1&username=testUser

but i can't make it work, any hint?

RewriteEngine on

RewriteCond %{QUERY_STRING} ^a=([^&]+)&u=([^&]+)&api_key=([^&]+)&api_key=([^&]+)&ip_address=([^&]+)$ [NC]
RewriteRule ^index\.php$ https://www.example.net/api.php?ip=%5&username=%2? [NC,L,R]
1 Answers

You have api_key parameter twice in your condition, moreover don't capture a value that you don't need to reuse in the target. You also have an extra misplaced ? at the end.

Have your rule like this:

RewriteCond %{QUERY_STRING} ^a=[^&]*&u=([^&]+)&api_key=[^&]*&ip_address=([^&]+)$ [NC]
RewriteRule ^index\.php$ /api.php?ip=%2&username=%1 [NC,L,NE,R=302]
Related