htaccess 301重定向-删除查询字符串(QSA)

时间:2021-07-28 10:38:38

I've been struggling with some htaccess redirects. I just spent some time reading and searching on stack and couldn't get an anwser that works with my scenario.

我一直在纠结一些htaccess重定向。我只是花了一些时间在堆栈上阅读和搜索,无法得到一个适合我的场景的答案。

I'm in the process of making the 301 redirect for an old client website to a new one. The old pages has parameters query which I want to remove from the url.

我正在为一个旧的客户网站做301重定向到一个新的客户网站。旧页面有我想从url中删除的参数查询。

/menu.php?idCategorie=29&idDetail=172

/ menu.php ? idCategorie = 29 &iddetail = 172

to

/new-website-page/

/ new-website-page /

I have multiple queries to do, here's a couple example:

我有多个查询要做,这里有几个例子:

/menu.php?idCategorie=29&idDetail=172
/menu.php?idCategorie=29&idDetail=182
/menu.php?idCategorie=29&idDetail=184
/menu.php?idCategorie=29&idDetail=256

Which all link to different new pages.

它们都链接到不同的新页面。

Here's what I tried:

这就是我试着:

RewriteCond %{QUERY_STRING} idDetail=172
RewriteRule ^menu.php(.*) /new-page/? [R=301,L]

I get redirected correctly, but the URL keeps the query string:

我被正确地重定向,但是URL保持查询字符串:

http://website.com/new-page/?idCategorie=29&idDetail=172

I also tried this:

我也试过这样的:

RewriteRule ^menu.php?idCategorie=29&idDetail=172$ http://website.com/new-page/? [L,R=301]

And this:

这:

RewriteCond %{QUERY_STRING} idDetail=172(.*)$
RewriteRule ^menu.php /new-page-name?$1 [L,R=301]

And it didn't work (Still have the query string at the end)

它没有工作(最后仍然有查询字符串)

Thanks!

谢谢!

2 个解决方案

#1


82  

You can use this rule:

你可以使用这个规则:

RewriteRule ^menu\.php$ /new-page-name? [L,R=301]

Take note of trailing ? in the end which is used for stripping off any existing query string in the original URI.

注意到末尾了吗?最后,它用于剥离原始URI中的任何现有查询字符串。

#2


25  

In addition to anubhava's answer you can alternatively use the QSD flag from Apache 2.4.0

除了anubhava的答案之外,您还可以使用Apache 2.4.0中的QSD标志

RewriteRule ^menu\.php$ /new-page-name [L,R=301,QSD]

http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsd

http://httpd.apache.org/docs/current/en/rewrite/flags.html flag_qsd

#1


82  

You can use this rule:

你可以使用这个规则:

RewriteRule ^menu\.php$ /new-page-name? [L,R=301]

Take note of trailing ? in the end which is used for stripping off any existing query string in the original URI.

注意到末尾了吗?最后,它用于剥离原始URI中的任何现有查询字符串。

#2


25  

In addition to anubhava's answer you can alternatively use the QSD flag from Apache 2.4.0

除了anubhava的答案之外,您还可以使用Apache 2.4.0中的QSD标志

RewriteRule ^menu\.php$ /new-page-name [L,R=301,QSD]

http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsd

http://httpd.apache.org/docs/current/en/rewrite/flags.html flag_qsd