I am trying to redirect a url with php key values to a different domain.
The site is being deprecated so I tried a simple redirect by hard coding the URL but could not get that to succeed.
I have also tried using RewriteCond
using QUERY_STRING
and RewriteRule
. I found success using only 1 key value here but when I tried using multiple key values, it does not pick up the second key and causes the redirect to fail.
RewriteCond %{QUERY_STRING} (|&)key1=value1($|&)
RewriteCond %{QUERY_STRING} (|&)key2=value2($|&)
RewriteRule (.*) https://www.example.com/new/path [L,R=301]
Expected result: example1.com/sample.php?key1=value1&key2=value2
--> example2.com/new/path
You need to use a single RewriteCond
to check your querystring before redirecting to the new site .
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)key1=value1&key2=value=2($|&) [NC]
RewriteRule ^/?sample\.php$ https://www.example.com/new/path? [L,R=301]
The empty question mark ?
at the end of the destination path is important as it discards the old querystring from new url otherwise if it removed then your new url will look something like example.com/new/path?key1=value1&key2=value2
.
Make sure to clear your browser cache before testing this new rule.