A WordPress site redirecting to spam is one of the most common signs of a hack, and one of the most frustrating, because the redirect often refuses to happen when you test it. Visitors complain about casino, pharmacy or fake prize pages, yet the homepage looks fine on your laptop. That is by design: redirect malware is written to avoid the people most likely to remove it.

This guide explains why the redirect is selective, the places in WordPress where the code usually hides, how to tell whether the attacker used a plugin flaw or stolen passwords, and how to clean up and let Google know the site is safe again.

Diagram of the conditions redirect malware checks before sending WordPress visitors to spam: device, referrer, login status and cookies
The redirect checks who is visiting first, which is why owners often cannot reproduce it.

Why is my WordPress site redirecting to spam only sometimes or on mobile?

Most redirect malware is conditional. Before it sends anyone away, it checks a few things about the visitor, and only redirects the ones who are unlikely to be the site owner:

Google's spam policies describe exactly this behaviour as sneaky redirects, including showing desktop users a normal page while redirecting mobile users to a spam domain. To reproduce it, use a phone on mobile data, open a private window, search for your business name and click through from the results.

Common places redirect malware hides

Redirect code needs to run on every page load, so it lives in places WordPress loads early or often. It rarely sits in just one place; attackers usually leave a second copy, or a small loader that rewrites the first if you delete it.

Checklist of places where WordPress redirect malware hides, from .htaccess and wp-config.php to injected database scripts, mu-plugins and cron jobs
Check every location on the list, because removing one copy often leaves another behind.

Before you change anything, take a full backup or hosting snapshot of the infected site. It feels odd to back up malware, but that copy is your evidence, and it lets you undo a cleanup step that breaks something.

Checking .htaccess, wp-config and the database

Start with the server-level files, because they act before WordPress even loads.

Compare, do not guess. If you are not sure whether a line belongs, compare the file with a fresh copy of the same WordPress version, or run wp core verify-checksums to list core files that differ from the official release.

Malicious JavaScript in themes, plugins and widgets

Many redirects run in the browser rather than on the server. Attackers append obfuscated JavaScript to the end of genuine theme or plugin scripts, add a script tag to header.php or footer.php, or paste code into a custom HTML widget. View the page source from a phone or a mobile emulator, and look for scripts loading from domains that have nothing to do with your site.

Illustration of a hidden compartment inside a server cabinet with malicious code coiled among the cables, representing hidden WordPress redirect malware
Redirect code is usually tucked inside files that already belong there, which makes it easy to miss.

Do not forget wp-content/mu-plugins. Must-use plugins load automatically and do not appear in the normal Plugins list, so a malicious file there can keep re-adding the redirect. Scheduled tasks can do the same; list them with wp cron event list and look for hooks with random-looking names.

Is it a plugin vulnerability or stolen credentials?

Knowing how the attacker got in matters more than finding every line of code, because the same door will be used again. The two most common routes are an exploited plugin or theme, and a stolen or guessed password.

If several sites in one hosting account are affected at the same time, suspect the hosting or SFTP credentials first.

Cleaning up and closing the entry point

Once you know where the code is and how it got there, clean in a sensible order so it cannot rebuild itself while you work:

Illustration of a heavy glowing door being bolted shut in a circuit wall, representing closing the entry point after a WordPress redirect hack
Cleaning without closing the entry point only buys time before the redirect returns.

The WordPress hardening guide's basics help here: keep everything updated, remove unused plugins and themes, and disable the built-in file editor. For the full order of work, see how to clean a hacked WordPress site and the WordPress malware cleanup checklist.

Telling Google the site is clean

If Google has spotted the problem, the Search Console Security issues report lists what it found, with sample URLs. Visitors may also see a browser warning. Fix everything first, then request a review from the report and explain briefly what you removed and how you closed the entry point.

Five-step flow for telling Google a WordPress site is clean after a spam redirect hack, from cleanup to requesting a review in Search Console
Request the review last, once the site is clean and the door is shut.

If the review fails, Google will usually tell you what is still wrong; treat that as a sign that a copy of the code is still present. My guide to removing Google's dangerous site warning covers the review process in more detail.

Frequently Asked Questions

Why is my WordPress site redirecting to spam only on mobile?

Redirect malware often checks the browser type and only redirects phones, because site owners usually test from a desktop. Test on a phone using mobile data and a private window, clicking through from a search result.

Why can't I see the redirect when I visit my own site?

Many redirects skip logged-in administrators, visitors who type the address directly, and people who have already been redirected once. Log out, use a private window and arrive from a search engine to reproduce it.

Will a security plugin remove a redirect hack?

A scanner may find known redirect code, but it can miss obfuscated scripts, database injections and the original entry point. Use it alongside clean reinstalls, database checks and a review of users and scheduled tasks.

Does a spam redirect hurt my Google rankings?

It can. Google treats sneaky redirects and hacked content as spam, and may show warnings or remove affected pages until the site is clean. Fixing it quickly and requesting a review limits the damage.

How do I stop the redirect coming back?

Find and close the entry point: update or remove the vulnerable plugin, rotate all passwords and security keys, delete unknown users and remove any auto-loading code in mu-plugins or cron.

Final Thoughts

A spam redirect that only appears for some visitors is not a mystery; it is malware doing its job of hiding. Reproduce it the way a real visitor would, check every place the code can live, find out how it got in, and close that door before you ask Google for a review. Work calmly and in order, and the redirect will not come back. If you would rather hand it over, my hacked site recovery service handles the full cleanup. For other early warning signs, read signs your WordPress site is hacked.

Official References

Sources checked on 29 October 2026: