301 redirect from a URL with a query string to a new domain with a different query string

I am trying to figure out how to do a 301 redirect in my htaccess file in order to redirect some files to a new domain. Here is what I need to know how to do this:

OLD URL: http://www.example.com/index.php?page=news&id=2366

NEW URL: http://www.example2.com/news.php?name=23546

Redirects do not have to be created automatically. I can hardcode the pages that I need to redirect in the htaccess file, I just don’t know which format to use, since I read that the β€œstandard” 301 redirect will not work with query strings.

Basically this is what I want to do, but from my research so far it does not sound like it can be done like that.

redirect 301 /index.php?page=news&id=2366 http://www.example2.com/news.php?name=23546 
+6
source share
2 answers

You can use a rewrite rule that matches the query string, for example:

 RewriteEngine On RewriteCond %{REQUEST_URI} ^/index.php$ RewriteCond %{QUERY_STRING} ^page=news&id=2366$ RewriteRule ^(.*)$ http://www.example2.com/news.php?name=23546 [R=301,L] 

Checkout this blog page for more information on how this works.

+18
source

I had the same problem, but even harder because I needed to drop other options.

Example: my-old-page.php?a=1&b=2&c=3

I need to use one of the lines and discard the rest, but this solution only works if I want to use the last parameter (c = 3). If I want to use any other (a = 1 or b = 2), it will work until 404. After long battles and searching, I found the answer:

 RewriteCond %{QUERY_STRING} ^.* ?b=2.* ?$ (without space after the *) RewriteRule (.*) http://www.my-webpage.php/new-location-2? [R=301,L] 

The solution is to add ".*?" before and after using the parameter.

I don't know if this is the best solution, but it works.

0
source

Source: https://habr.com/ru/post/913106/


All Articles