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

夙愿已清 提交于 2019-11-26 17:48:16

问题


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

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

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

The redirects don't have to be automatically created. I can hard-code the pages I need to redirect in the htaccess file, I just don't know the format to use as I've read that a "standard" 301 redirect won't work with query strings.

Basically this is what I want to do, but from my research so far it doesn't sound like it can be done this way.

redirect 301 /index.php?page=news&id=2366 http://www.example2.com/news.php?name=23546

回答1:


You could use a rewrite rule with a query string match condition, such as:

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.




回答2:


I had the same problem, but still more complicated, because I needed to discard other parameters.

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

I need to use one of the strings and discard the others, but that solution only worked if I want to use the last parameter (c=3). If I want to use any other (a=1 or b=2) it runs to a 404. After much struggling and searching, I found an 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 the parameter to use.

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



来源:https://stackoverflow.com/questions/10135702/301-redirect-from-url-with-query-string-to-new-domain-with-different-query-strin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!