.htaccess redirect to "410 Gone" status for URLs containg one letter + word "shop" after domain

.htaccess redirect to "410 Gone" status for URLs containg one letter + word "shop" after domain - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, apache, apache2, 410-gone, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


After I recovered Drupal 7 site from malware attack I end up with lot of links pointing to (previously) spam content like this:



https://www.example.com/lshop/puma-rihanna-c-449/?zenid=id311p8tc67mbnbu8gb17d1uf1
https://www.example.com/eshop/nike-start-l-259/
https://www.example.com/eshop
https://www.example.com/fshop/adidas-maradona-k-149/


Content was removed but backlinks left. I was able to compose this rules in .htaccess redirecting malicious backlinks to 410 Gone status page:



<IfModule mod_rewrite.c>
redirect 410 /ashop/
redirect 410 /ashop

redirect 410 /bshop/
redirect 410 /bshop

redirect 410 /cshop/
redirect 410 /cshop

redirect 410 /eshop/
redirect 410 /eshop

redirect 410 /fshop/
redirect 410 /fshop

redirect 410 /ishop/
redirect 410 /ishop

redirect 410 /lshop/
redirect 410 /lshop

redirect 410 /oshop/
redirect 410 /oshop

redirect 410 /pshop/
redirect 410 /pshop
</IfModule>


List can be longer. How to catch - using regex - pattern "backslash after domain + one letter + word 'shop' optionally continuing with backslash" and redirect such link to 410 on server level befor it touch Drupal?



I tried following patterns based on these two answers but without success:



redirect 410 /[a-z]shop/
redirect 410 /^([a-zA-Z])shop/
redirect 410 ^/([a-z])(shop)/(.*)$
redirect 410 ^[a-z]shop+$

Solution :

redirect is a mod_alias directive (not mod_rewrite) and does not accept a regex. Both the answers you link to use the mod_rewrite RewriteRule directive.



Note that in .htaccess the RewriteRule pattern does not match against the slash at the start of the URL-path (unlike a mod_alias Redirect or RedirectMatch directive).



For example:



RewriteRule ^[a-z]shop($|/) - [G]


The above matches either a request for /xshop or /xshop/<anything> (where x is any lowercase letter) and serves a 410 Gone.



This needs to go near the top of your .htaccess file and the <IfModule> wrapper is not required.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect