Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

regex - htaccess 301 redirect - Remove query string (QSA)

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.

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.

/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:

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...