Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 911
newpxsn Avatar asked Jan 14 '14 16:01

newpxsn


People also ask

Can a 301 redirect be removed?

The short answer is "yes." You can reverse a 301-redirect, even though it's technically permanent. The long answer, though, is that this change may not work the way you'd expect or hope, and it could even make your problems worse.

Should I enable 301 .htaccess redirect?

The . Because the WordPress 301 redirect is not always reliable, we recommend issuing the 301 redirect via your . htaccess file. Another benefit is that the . htaccess redirect is slightly faster than redirecting via PHP, because it is loaded even before the rest of the page.

What is a 301 .htaccess redirect?

A 301 Permanent Redirect permanently redirects one URL to another. You set up a 301 redirect using . htaccess to send visitors to a new URL and tell search engines that a page has moved so that the new page can be properly indexed.


2 Answers

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.

like image 189
anubhava Avatar answered Oct 02 '22 17:10

anubhava


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

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

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

like image 31
RafaSashi Avatar answered Oct 02 '22 16:10

RafaSashi