Htaccess redirect [F] that matches both a directory name and a variable

Viewed 16

I want to completely block requests for urls in this format:

/cat/pathfinder/pathfinder-accessories/?add-to-cart=27368

or

/cat/pathfinder/?add-to-cart=27368

I tried to do this via HTaccess :

RewriteRule ^(.*)cat$^(.*)?add-to-cart=$^(.*)$ - [F]

But that doesn't match. Can you see why not?

I have other, simpler rewriterules in the same .htaccess file that are working.

Context:

I have an online shop site being pounded by automated requests to 'add to cart'. The shop has thousands of products, so the impact of so many simultaneous requests on memory & processor is horrible. (The shop is Wordpress with Woocommerce, if that matters)

I changed things so that there are no longer pages of 20 products where the bot can hit 'add to cart' simultaneously on all of them any more, but they are still hitting them, so clearly they have a list. I've been unable to block the requests by IP, as they are coming via Cloudflare from many locations.

1 Answers

Query string is not matched using RewriteRule.

You can use this rule instead:

RewriteCond %{THE_REQUEST} /cat/[^?]*\?add-to-cart=\d+ [NC]
RewriteRule ^ - [F]
Related