简体   繁体   English

htaccess 301 redirect - 删除查询字符串(QSA)

[英]htaccess 301 redirect - Remove query string (QSA)

I've been struggling with some htaccess redirects. 我一直在努力解决一些htaccess重定向问题。 I just spent some time reading and searching on stack and couldn't get an anwser that works with my scenario. 我只是花了一些时间在堆栈上阅读和搜索,并且无法获得适用于我的场景的anwser。

I'm in the process of making the 301 redirect for an old client website to a new one. 我正在将旧客户端网站的301重定向转换为新的客户端网站。 The old pages has parameters query which I want to remove from the url. 旧页面有参数查询,我想从网址中删除。

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

to

/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! 谢谢!

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中的任何现有查询字符串

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

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

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