简体   繁体   English

如何使用查询字符串重定向URL

[英]How to redirect a URL with a querystring

I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs. 我已将网站移至新域,并使用RewriteRule重定向了许多URL。

I'm stuck trying to redirect URLs with query strings in them? 我在尝试使用其中的查询字符串重定向URL吗?

RewriteRule  /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]

Further up the htaccess file I'm using this to redirect the domain 进一步访问htaccess文件,我正在使用它来重定向域

RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]

When I visit: 当我访问时:

old-website.com/news/archive.php?cat=economic-impact

it redirects to 它重定向到

new-website.com/news/archive.php?cat=economic-impact

Which 404s, I need to to redirect to: 我需要重定向到哪个404s:

new-website.com/faqs/economic-impact

Can anyone help? 有人可以帮忙吗?

You need to add a blank query string on your rewrite to truncate any existing query strings. 您需要在重写中添加一个空白查询字符串以截断所有现有查询字符串。

The below should do the job: 下面应该做的工作:

RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]

In addition, the way you're checking the existing query strings won't work. 此外,检查现有查询字符串的方式将不起作用。 You'll need to check them separately: 您需要单独检查它们:

RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]

The %1 will take a backreference from a regex group match in the RewriteCond %1将从RewriteCond的正则表达式组匹配中进行反向引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM