0

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

Eddie
  • 26,593
  • 6
  • 36
  • 58
Ralix14
  • 3
  • 2
  • You could also solve this in PHP as explained in: https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php – Mister Tee Apr 18 '19 at 16:07

1 Answers1

0

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.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • RewriteCond %{QUERY_STRING} (^|&)key1=value1&key2=value2($|&) [NC] RewriteRule (.*) https://www.example.com/app/path? [L,R=301] . I have the same issue as before where if I have a second key value, it redirects back to https://www.example.com/key1. With only 1, it redirects to the exact page I want. Is the (.*) causing this issue? – Ralix14 Apr 18 '19 at 17:16
  • @KevinBraga Try clearing your browser cache. Also if there are other similar rules then try removing them just to see if that solves it. – Amit Verma Apr 18 '19 at 17:43