Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritingquery-string

Redirect url with specific query string to home page using htaccess


I have a hacked website where a hacker has injected e-commerce pages.

The pages are ranked on google but I want to redirect them to the homepage.

They have the following URL structure. The digits are randomly generated and they are almost 1000 on SERP. I have put random digits as an example:

https://example.com/?itemywdy11111111.html

https://example.com/?itemywdy51111.html

Below is my htaccess

RewriteEngine On

RewriteCond %{QUERY_STRING} ^itemywdy$
RewriteRule (.*) / [QSD,R=302,L]

The code above is not working at all.

How do I do a 301 redict using htaccess for these urls.


Solution

  • You should match only the starting part of query string leaving end anchor:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (?:^|&)itemywdy [NC]
    RewriteRule ^ / [QSD,R=301,L]
    

    Also note use of (?:^|&) to let this string match anywhere in a query string which would allow this rule to work for your existing pages with other query strings as well.