Search code examples
.htaccesshttp-redirectredirecttoaction

301 redirect with ? (question mark) not working in htacces


This is probably an easy question, but we can not find why the 301 is not working. When we have a url with a question mark the 301 redirect in our .htacces is not working. For example:

/order/order.html?AddID=1078&Rand=666171759380936096

so: Redirect 301 /order/order.html?AddID=1078&Rand=666171759380936096 http://www.domain.nl

In our webmaster tools we have 8000 url's with the same structure /order/order.html?AddID=.... that say 404 not found. We want to 301 redirect them to the homepage, but we get a 404 not found page instead. when we use the same redirect with only /order/order.html he is redirected correct.


Solution

  • You can't match against the query string in a Redirect statement, use mod_rewrite and match against the %{QUERY_STRING} var:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^AddID=1078&Rand=666171759380936096$
    RewriteRule ^order/order.html$ http://www.domain.nl/? [L,R=301]
    

    But since you have like 8000 URLs that start with the query string ?AddID=, then you can just match against that:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^AddID=[0-9]
    RewriteRule ^order/order.html$ http://www.domain.nl/? [L,R=301]