Rewrite rule with query string

1.7k Views Asked by At

Need a little help with rewrite rule. How do I redirect an url with query to another url with query? Example:

http://example.com/prject/test.php?key=1

redirect to

http://example.com/prject/test.php?id=1

Also

http://example.com/prject/file2.php?key=1

to

http://example.com/prject/file2.php?arg=1

.htaccess file is located in /project/ directory.

2

There are 2 best solutions below

0
On BEST ANSWER

In your /project/.htaccess file, insert the following:

RewriteEngine On
RewriteBase /project/

# Redirect test.php?key=<number> to test.php?id=<number>
RewriteCond %{QUERY_STRING} key=(\d+)
RewriteRule ^test.php$ test.php?id=%1 [R,L]

# Redirect file2.php?key=<number> to file2.php?arg=<number>
RewriteCond %{QUERY_STRING} key=(\d+)
RewriteRule ^file2.php$ test.php?arg=%1 [R,L]
3
On

Try this :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^key=([^&]+) [NC] 
RewriteRule ^ http://example2.com/prject/test.php?key=%1 [NC,R,L]