Sample alert How it works Coverage PENTEST METHODOLOGY DOCS PRICING FAQ MCP CONTACT LOG IN SCAN MY APP →

WordPress site redirects to spam: how to find and remove it

FIX / WP MALICIOUS REDIRECT / 10 MIN READ

A WordPress site that redirects some visitors to spam has injected code in a file, in the database, or in an unknown plugin. Find the code and reinstall clean core and plugin files. Remove unknown admin users and change every password and key. Do the work in this order. If you only remove the visible code, the redirect returns.

This guide shows how to confirm the redirect, where to look, which WP-CLI commands to use, and how to ask Google to remove the warning.

How do I confirm the redirect?

The redirect is often selective. The code checks who the visitor is and hides from the owner. It can redirect only mobile browsers, only visitors from Google, only visitors who are not logged in, or only the first visit.

Test with curl. Change the user agent and the referrer, and read the redirect headers:

# Normal desktop request
curl -sIL https://your-site.example/ | grep -iE "^(HTTP|location)"

# Mobile browser from a Google search
curl -sIL https://your-site.example/ \
  -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148" \
  -e "https://www.google.com/" | grep -iE "^(HTTP|location)"

A Location header to a domain that you do not own is a server-side redirect. The cause is in .htaccess, in PHP code, or in the web server configuration.

If the headers look normal and the browser still redirects, the cause is JavaScript. Save the page and search it:

curl -sL https://your-site.example/ \
  -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)" \
  -e "https://www.google.com/" -o page.html
grep -oE "<script[^>]*src=[^>]+>" page.html | sort -u
grep -nE "eval\(|atob\(|String\.fromCharCode|window\.location|document\.location" page.html

Look for <script> tags from domains that you do not know. Also look for long strings that atob or eval decode.

Caution: Open the redirect target only in a test browser profile or in a virtual machine. Some spam pages try to install software or steal logins. Do not click anything on them.

Also check the simple causes before you blame malware:

  1. Open Settings, then General. The WordPress Address and Site Address must be your domain.
  2. Disable the caching plugin and the CDN for a test. A stale cache can show old bad code after you fix it.
  3. Check the DNS records at your registrar. A changed A record sends visitors to another server.

How do I make a safe copy first?

Make a copy before you change anything. Store it outside the web root, so nobody can download it. See the backup files guide for the reason.

cd /path/to/wordpress
wp db export ~/before-clean.sql
tar -czf ~/before-clean-files.tgz .

Keep the copy for evidence. Do not restore from it later. It contains the bad code.

Where is the injected code?

Check these places in this order. Run the commands over SSH in the WordPress folder.

Which core files changed?

WP-CLI compares your core files with the official checksums from WordPress.org. The check runs before WordPress loads, so malware cannot hide from it.

wp core verify-checksums
wp core verify-checksums --include-root

The command lists files that changed, files that are missing, and, with --include-root, extra files in the root. A file such as wp-content-backup.php or wp-tmp.php in the root is suspect. If the output shows a version error, add --version and --locale with the values from Dashboard, then Updates.

For plugins that come from WordPress.org, run the same kind of check:

wp plugin verify-checksums --all

Plugins from other sources cannot be checked this way. Compare them with a fresh download from the vendor.

What is in .htaccess?

On Apache, .htaccess is a common target. A normal WordPress file has only this block:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Look for RewriteCond lines that test HTTP_USER_AGENT, HTTP_REFERER, or Googlebot, followed by a RewriteRule that sends the visitor to another domain:

# Malicious example: do not use
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?(google|bing)\. [NC]
RewriteRule ^(.*)$ https://spam.example/ [R=302,L]

Remove every such line. Also check for extra .htaccess files in sub-folders:

find . -name ".htaccess" -not -path "./wp-content/plugins/*"

What is in wp-config.php and the theme?

Open the first 20 lines of wp-config.php. Attackers add code at the top, before the database settings. Also search all PHP files for common obfuscation:

grep -rEl "eval\s*\(\s*(base64_decode|gzinflate|str_rot13)|preg_replace\s*\(.*/e['\"]" . --include=*.php
grep -rEl "base64_decode\s*\(" wp-content/themes wp-content/plugins --include=*.php

Many clean plugins use base64_decode, so read each hit. A hit that decodes a long string and runs it is a strong sign of malware.

Then look for PHP files where none belong, and for recent changes:

find wp-content/uploads -name "*.php"
find . -name "*.php" -mtime -14 -not -path "./wp-content/cache/*"

PHP files in uploads are almost always bad. Check the functions.php and header.php of the active theme and of every child theme. Attackers add a script loader there.

What is in the database?

Injected scripts also live in the database, so a file reinstall does not remove them. Check the site address first:

wp option get siteurl
wp option get home

Then search the whole database for script tags and decoders:

wp db search '<script' --all-tables
wp db search 'eval(' --all-tables

Read the table and the column of each hit. Typical places are wp_posts, wp_options, and the theme settings. A widget with a <script> tag from an unknown domain is a common one. Remove the tag from the post or option with the dashboard or with wp post update and wp option update.

Warning: Do not run a global wp search-replace to remove a script. A wrong pattern damages serialized data. Edit the single row that you found.

How do I find hidden admin users and plugins?

Attackers keep a way back in. Look for it before you clean the files.

List the administrators:

wp user list --role=administrator --fields=ID,user_login,user_email,user_registered

Delete users that you do not know. Give their content to a real user:

wp user delete 7 --reassign=1

Some malware hides a user from the dashboard list. Query the database directly and compare:

wp db query "SELECT u.ID, u.user_login, u.user_registered FROM wp_users u JOIN wp_usermeta m ON m.user_id = u.ID WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%'"

Change wp_ to your table prefix if it is different. The users endpoint guide shows how attackers get the login names in the first place.

Then check the plugins that the dashboard hides:

wp plugin list --fields=name,status,version
ls -la wp-content/mu-plugins wp-content/plugins

The folder wp-content/mu-plugins holds must-use plugins. WordPress loads them on every request, and the Plugins screen shows them on a separate tab. A normal site has few or none. Remove any file there that you do not know. Also remove plugins that you did not install, and plugins that you do not use.

Finally, check the scheduled tasks. Malware uses them to come back:

wp cron event list --fields=hook,next_run_relative

Delete a hook that you do not know with wp cron event delete <hook>.

How do I reinstall clean files?

Replace the core and the plugins with clean copies. This removes bad code that you did not find.

  1. Reinstall core. This keeps wp-content and wp-config.php:
wp core download --force --skip-content
  1. Reinstall each plugin from WordPress.org:
for p in $(wp plugin list --field=name); do wp plugin install "$p" --force; done

A plugin that is not on WordPress.org fails here. Download it again from the vendor, and delete the old folder first.

  1. Reinstall the theme. Use wp theme install <theme> --force, or upload a clean copy. Compare any custom theme with your Git history.
  2. Delete the extra files. Remove each unknown PHP file that the earlier checks found. Delete plugins and themes that you do not use.
  3. Reset .htaccess. Use the default block from this guide. In the dashboard, open Settings, then Permalinks, then select Save Changes to write it again.
  4. Run the checks again. wp core verify-checksums and wp plugin verify-checksums --all must report no error.

If you find too many changed files, or you do not trust the result, rebuild the site. Install a fresh WordPress, then restore only the content from a clean export. Use a backup from a date before the first redirect.

How do I find the hole that the attacker used?

A clean site gets hacked again if the hole stays open. Look for the cause.

  • An old plugin or theme. Read the changelog of each plugin. Check each name in a vulnerability database. Wordfence publishes a weekly vulnerability report that lists new problems in plugins and themes.
  • A plugin that AI wrote. Code with no checks on the user or the nonce gives access to a stranger. Read the AI-written plugin guide.
  • A weak password. Check the access logs for many POST requests to wp-login.php and xmlrpc.php from one address.
  • A stolen key. A leaked FTP or SSH password gives full access. Check the last logins on your host.
  • A shared host. If another site on the same account was hacked, it infects yours. Split the sites to separate accounts.

Read the access log around the date of the first file change. Look for requests to a PHP file in uploads or to a plugin file that you do not know.

How do I change all secrets?

Do this after the files are clean. If you change secrets first, the attacker still has the old way in.

  1. Change the passwords of every administrator, editor, and author:
wp user update 1 --prompt=user_pass
  1. Change the database password. Do it in the hosting panel, then update DB_PASSWORD in wp-config.php:
wp config set DB_PASSWORD 'new-long-random-password'
  1. Change the salts. This logs out every user, including the attacker:
wp config shuffle-salts
  1. End all sessions. Run wp user session destroy <user> --all for the main users.
  2. Change the hosting, FTP, SSH, and email passwords. Also change API keys that the site stores, for example payment, email, and AI keys.
  3. Turn on two-factor login for administrators.
  4. Set file permissions. The WordPress hardening guide advises 755 for folders, 644 for files, and 400 or 440 for wp-config.php.

If the site was a target together with other apps, rotate the keys of those apps too. The hacked app guide gives a general order for this work.

How do I remove the warning in Google?

Visitors from Google can see a “Deceptive site ahead” or “This site may be hacked” warning. Clean the site first. Then ask for a review.

  1. Open Google Search Console and select your property.
  2. Open the Security Issues report. The Google help page describes it.
  3. Read the list of examples. Check that each example URL is clean.
  4. Select Request review. Describe what you removed and what you changed.
  5. Wait for the result. Search Console shows the result in the report.

Also check the Manual actions report. Spam pages that the attacker added can cause a manual action. Remove those pages, and submit a request there too.

Search for site:your-site.example in Google. Spam pages in the results show that the attacker added URLs. Remove them and return 410 for them.

How do I stop this in the future?

  1. Update on a schedule. Update core, plugins, and themes each week. Turn on automatic updates for security releases.
  2. Delete what you do not use. An inactive plugin still has files that an attacker can call.
  3. Use few plugins. Each plugin is a part of the attack surface.
  4. Limit admin accounts. Give each person the lowest role that works.
  5. Protect login. Use two-factor login and a limit on login attempts.
  6. Store backups off the server. Keep a clean copy from before an attack.
  7. Block PHP in uploads. Add this to wp-content/uploads/.htaccess on Apache:
<FilesMatch "\.php$">
  Require all denied
</FilesMatch>

On nginx, add this inside the server block:

location ~* ^/wp-content/uploads/.*\.php$ { deny all; }
  1. Scan the live site. Exposed files and open endpoints let attackers in. The backup files guide covers one common case.

When should I get help?

Get help in these cases:

  • The redirect returns after you clean the site. A hidden file or a server-level problem remains.
  • The site stores payment data or personal data. You may have a duty to tell users.
  • Other sites on the same server show the same problem.
  • You cannot get SSH access and your host does not help.
  • Google does not remove the warning after two reviews.

A security engineer or a malware cleanup service can do a full check of the server. After the cleanup, repeat the curl tests from the first section. Every test must return your own site and no redirect.

Common questions

Why does the redirect not happen when I visit my own site?
The malware hides from logged-in users and from direct visits. It redirects only mobile browsers, search visitors, or first-time visitors, so the owner does not see it.
Is it enough to delete the bad code I find?
No. The attacker often leaves a second way in, such as an unknown admin user or a hidden plugin. Remove all of them, then change all passwords and keys.
Do I need to delete the whole site and start again?
Not always. Reinstall core and plugins from clean copies, remove unknown files, and clean the database. Rebuild from a clean backup if you cannot trust the files.
Does a security plugin fix the problem?
A scanner helps to find bad files. It does not close the hole that the attacker used. Update the plugin or theme that the attacker used, and remove it if it has no fix.
How long does Google show the warning?
Google removes the warning after it reviews your request, and you must clean the site first. Open the Security Issues report in Search Console and request a review.

Check the live site

A VibeEval scan tests the live site from outside. It reports exposed files, open endpoints, and public user lists that attackers use to get in.

14-day free trial · Cancel anytime

SCAN YOUR APP →