.htaccess redirect without query parameters

2019-08-03 07:27发布

问题:

I changed a website and now Google webmaster is loading hundreds of "Not Found" URLs:

http://www.domain.com/images/picture.php?idphotos=720,404,,4/1/13,Not found

The picture.php does not exist anymore so I created a .htaccess entry to redirect the root of the site:

Redirect 301 /images/picture.php. /

Now the problem is that any query parameters are also sent in the redirect:

http://www.domain.com/?idphotos=720

How can I create a redirection which doesn't include the query parameters, e.g. like this:

http://www.domain.com/

回答1:

Try this (note the trailing ?):

RewriteCond %{QUERY_STRING} !=""
RewriteRule . http://www.domain.com/?


回答2:

Alternatively use the QSD flag from Apache 2.4.0

RewriteCond %{QUERY_STRING} !=""    
RewriteRule . http://www.domain.com/ [L,R=301,QSD]

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