How to Clean a Hacked WordPress Site Without Losing SEO

How to Clean a Hacked WordPress Site Without Losing SEO

To clean a hacked WordPress site without losing SEO, treat it as three separate jobs done in sequence: remove the malicious payload, close the way it got in, and keep search engines from drawing the wrong conclusions while you work. The order matters, because deleting a backdoor before you understand the entry point usually means the site is reinfected within days, and that second infection is what does the real ranking damage.

Most SEO loss during a hack comes from one of four things: the host or a scanner blacklisting the domain, search results showing injected spam titles and descriptions, Google returning a security interstitial on your URLs, or a cleanup that accidentally strips canonical tags, redirects or content. None of these are caused by the malware itself so much as by how the recovery is handled. Work methodically and you can usually keep the same URLs, the same content and the same rankings.

Step one: identify what is actually wrong before you touch anything

Start by taking a full snapshot of the files and the database. You want a forensic copy you can compare against later, and you want it before you delete a single file. On a server with shell access, archive the document root and dump the database:

tar czf /tmp/site-before.tar.gz -C /var/www site
mysqldump --single-transaction site_db > /tmp/site-before.sql

Then establish what the site is serving. Fetch the homepage and a few deep URLs with a normal user agent and look at the raw HTML, not the rendered page. Injected spam is frequently served only to search engine crawlers, keyed on the User-Agent header, or only to visitors arriving from a referrer. Compare the two:

curl -s -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" https://example.com/ | grep -i "<title>\|<meta name=\"description\""
curl -s -A "Mozilla/5.0 (X11; Linux x86_64)" https://example.com/ | grep -i "<title>\|<meta name=\"description\""

If the titles differ, you have cloaking. If they match but both contain links to an unfamiliar domain, you have a straight injection. Check the response headers too, for a Location header or an unexpected X-Redirect-By that would send mobile visitors elsewhere. Then check your domain against the public safe browsing and transparency reports by searching for your own hostname, and check the search console for manual actions and security issues. Knowing which of these apply tells you how much urgency to apply to the rest.

How to clean a hacked WordPress site: remove the payload

WordPress core files are deterministic. Download a fresh copy of the same version from the official release archive, then diff it against your installation. Anything that differs in wp-admin, wp-includes or the root PHP files is suspect, with two exceptions: wp-config.php and wp-content, which are yours to modify. Replace the core files wholesale rather than patching them. Then reinstall all plugins and themes from their original sources, deleting the existing directories first so that a file present in your copy but absent from the clean package cannot survive.

For wp-content and the database, you need to find the actual payloads. Grep for the patterns attackers commonly use. Base64 blobs, eval, gzinflate, str_rot13 and long hex strings are the usual suspects, though a competent attacker will avoid all of them:

grep -rIl --include="*.php" -E "eval\(|base64_decode|gzinflate|str_rot13|assert\(" /var/www/site/wp-content

Treat that as a starting point, not a guarantee. Compare file modification times against your last known good backup, since a recently modified file in a plugin directory that you did not update is a strong signal. In the database, search the wp_options, wp_posts and wp_postmeta tables for script tags, iframes and unfamiliar domains. Remember that a single injected option can load JavaScript on every page, so the database is where a lot of SEO damage actually lives.

When you remove content, remove the injected markup only. Do not rewrite your own post titles or descriptions, and do not change permalinks. If a post was created by the attacker, delete it and make sure it returns a 410 or 404 rather than redirecting somewhere unexpected.

Close the entry point, or you will do this again

Look at the access logs for the period before the infection. You are looking for POST requests to wp-login.php, xmlrpc.php and admin-ajax.php, and for requests to PHP files in wp-content/uploads, which should never execute. If uploads are executing PHP, that is often the whole story. Block it at the web server level. For Apache, in the uploads directory or a parent config:

<Directory "/var/www/site/wp-content/uploads">
    <FilesMatch "\.php$">
        Require all denied
    </FilesMatch>
</Directory>

For nginx, the equivalent is a location block matching \.php$ under /wp-content/uploads/ that returns 403. Then rotate every credential: database password in wp-config.php, all WordPress user passwords, all salts and keys in wp-config.php (regenerating these invalidates every existing session cookie, which logs the attacker out), FTP and SSH credentials, and any API keys stored in plugins. Remove any WordPress user account you do not recognise, and check the wp_usermeta table for wp_capabilities rows granting administrator to an unexpected user ID.

Finally, check for scheduled tasks and persistence outside WordPress: cron entries for the site user, files in /tmp owned by that user, and any .htaccess or php.ini overrides that were not there before. Attackers frequently leave a small loader that re-drops the payload after cleanup.

Protect your rankings while you recover

If the site is serving injected content, returning errors, or showing a browser warning, the fastest way to limit damage is to stop serving the bad pages. A 503 with a Retry-After header tells crawlers the outage is temporary and that they should come back, rather than treating the pages as gone:

HTTP/1.1 503 Service Unavailable
Retry-After: 86400

Serve that for the whole site only if you genuinely cannot serve clean pages. If only some URLs are affected, let the clean ones keep returning 200. Never use 410 or a redirect to a placeholder page across the whole site, because that is what actually removes pages from the index and takes months to undo.

Once the site is clean, verify it against the safe browsing report again, then request a review in the search console. Submit a fresh sitemap. Check that your canonical tags, hreflang annotations, structured data and internal links are intact, since a rushed theme reinstall is a common way to lose them. Keep the site stable for a while: no redesigns, no permalink changes, no mass redirects in the same period. Give the crawlers a consistent target to re-evaluate.

After the immediate work is done, set up file integrity monitoring so that a modified core file or a new PHP file in uploads generates an alert rather than being discovered weeks later. Keep offsite backups that you have actually restored from at least once, and keep a short written record of what the entry point was, because the next person to look at this, possibly you, will want it.

Related articles

Subscribe to our newsletter

Get the latest hosting tips, performance insights, and industry news.