Bruno B
Bruno B

Reputation: 21

Wordpress - Force 404 Errors in Htaccess

My website got hacked a few weeks ago and now I'm on the stage of removing all spammy links from my page. Some of them are 301 redirects to legit webpages of my site, which is affecting my SEO.

Example:

However, I'm not sure how to put this in .htaccess. Everytime I try something, my whole website gets a 404 error or the images don't appear, etc.

Here's what I've tried

  1. Add this on the bottom of the file (which does not work):

    ErrorDocument 404 /z
    ErrorDocument 404 /?attachment_id=93**
    
  2. Add the following within the <IfModule mod_rewrite.c> code tags.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    ....
    RewriteCond %{REQUEST_URI} !^/(?attachment_id=93|z)$ [NC]
    RewriteRule ^ - [L,R=404]   
    </IfModule>
    

This makes the entire website not to work!

I appreciate any help you can provide!

Upvotes: 2

Views: 393

Answers (1)

MrWhite
MrWhite

Reputation: 45829

ErrorDocument 404 /z

The ErrorDocument defines a custom error document, so this is indeed invalid. But this is not needed, if you just want to reject spammy/invalid URLs.

Add the following within the <IfModule mod_rewrite.c> ...

You also need to place these directives before your existing WordPress directives. Apart from being wrong, if you place them after the WordPress front controller they will never be processed anyway.

However, if you have already "fixed" your hacked site then these URLs (presumably invalid) should not be redirecting and they should already be resulting in 404s? If you are still seeing a redirect then this might be a cached redirect from your browser cache.

Anyway, in order to get Google to drop these URLs quicker from the search results then it would perhaps be better to return a 410 Gone instead. With the two examples in your question, you will need two rules in .htaccess. Something like the following:

# www.example.com/?attachment_id=93
RewriteCond %{QUERY_STRING} ^attachment_id=93$
RewriteRule ^$ - [G]

# www.example.com/z
RewriteRule ^z$ - [G]

Upvotes: 1

Related Questions