1

My website has been spammed by hackers for maybe 1 year. So now I have to update everything I think. And it seems that the page targetted by spammers is going to 404.

My question : I tested this command on htaccess to redirect 404 page to 410 but it's not working.

The spammed urls are like this

example.com/date-women..
example.com/need-women..
example.com/need-men-usa
example.com/old-women..

(Where example.com is my website.)

So I need to ask htacess to redirect 404 of these spams to 410

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# 410 gone for a long string of lowercase letters 
# and/or numbers followed by an optional long extension
# to handle spam URLs like /spstyaaliti4csf6ne.desiringly
RewriteRule ^/?[date](\.[women]?$ - [G,L]

it's not working?

I have about 500 pages to declare on Bing and Google Search Console. So it's better to erase them using 410 with htaccess.

Can you help ?

3
  • It's not clear what you are trying to attempt. Are you trying to redirect all urls with either date or women in them? If so, urls with men will still get through. Another way may be to just redirect all 404s to 410s. Feb 12 at 11:09
  • How many actual URLs/pages do you have on your site? If you don't have many, it might be easier to whitelist the "good" URLs and 410 the rest? What type of site is this? Static files or front-controller/CMS?
    – MrWhite
    Feb 12 at 11:24
  • You mention "...search console". Are these URLs actually indexed and appearing organic search results?
    – MrWhite
    Feb 12 at 13:06

1 Answer 1

0

To redirect all urls containing date or women, this should work -

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# 410 gone for a long string of lowercase letters
# and/or numbers followed by an optional long extension
# to handle spam URLs like /spstyaaliti4csf6ne.desiringly
RewriteRule ^(date|women) - [G,L]
  • You don't want the dollar as it signifies that the pattern should be at the end of the url.
  • You can add other words.
  • Because you have already checked that such a file, directory, or link does not exist, this should be fine.

As mentioned, this was an example for the two words you mentioned. You can add more words to the last line, added child

RewriteRule ^(date|women|child) - [G,L]

But if the URLS do not form many patterns then the best thing to do would be to redirect all URLs. You have already checked that it's not a file, directory, or link. This means it's an invalid URL. So just redirect them all.

RewriteRule ^(.*)$ - [G,L]

To raise a 410 error, replace with this

RewriteRule ^(.*)$ - [R=410,L]
2
  • ^(date|women) - This is checking that the URL starts with date or women, followed by anything (which might be part of the same "word"). To match whole words anywhere you should probably use word boundary \b, although from the look of the URLs in the question, only - (hyphen) is being used to separate words, so something like (^|-)(date|women)($|-) instead (although I would have thought date is likely too common)?
    – MrWhite
    Feb 12 at 12:53
  • "it's not a file, directory, or link. This means it's an invalid URL." - That assumes the site is entirely static (not "dynamic" where the site is using a front-controller/CMS like WordPress, Joomla, etc.) On a "dynamic" site the filesystem checks are probably redundant. Even on a static site they could probably be avoided.
    – MrWhite
    Feb 12 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.