WordPress Security

WordPress Redirecting to Spam Only on Mobile or From Google? How to Find It

By Rajan Gupta

• ⏱ 6 min read

"It works fine for me" is the most common sentence I hear before finding a conditional redirect. The site loads normally on the owner's laptop. Then a customer says they got sent to a casino page from their phone, or Search Console shows a spike in traffic that goes nowhere. The malware is working exactly as designed: it only fires for people who aren't you.

This post is about the part most guides skip. Before you can remove a conditional redirect, you have to reproduce it. Below is a way to do that from the command line, and a list of the places it usually hides once you can see it.

Why you can't see it

Redirect malware wants to stay alive, so it avoids the people most likely to notice it. It checks a few things before it acts:

  • Device. Phones and tablets get redirected, desktop doesn't. Owners tend to work on desktop.
  • Referrer. Only visitors arriving from Google, Bing or social media are redirected. Typing your address directly does nothing.
  • Login state. Logged-in users are skipped, so an admin never sees it.
  • First visit only. A cookie records that you've been redirected once, so a second look comes up clean.
  • Location or IP. Traffic from the site owner's country, or from known security scanners, may be left alone.

That combination means a visual check will often come back clean even on a badly infected site.

Reproduce it from the command line

You can imitate a phone arriving from Google using curl. This asks for the page and prints only the status lines and any redirect destination:

# Pretend to be an iPhone arriving from Google
curl -sIL -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/" https://example.com/ | grep -iE "^HTTP|^location"

# Compare with a plain desktop request
curl -sIL https://example.com/ | grep -iE "^HTTP|^location"

If the first command prints a Location: header pointing somewhere you don't recognise and the second doesn't, you've reproduced it. Try a second pass with a Googlebot user agent as well. Some infections serve spam pages to search engines and normal pages to people, which is why a site can look fine while Google indexes hundreds of junk URLs.

Not every redirect happens in HTTP headers. Many are injected JavaScript. To see the scripts the server sends to a mobile visitor from Google:

curl -sL -A "Mozilla/5.0 (Linux; Android 14; Pixel 8) Mobile" -e "https://www.google.com/" \
  https://example.com/ | grep -oE "<script[^>]*src=[^>]*>" | sort -u

Any script from a domain you don't recognise deserves a closer look. If curl from your own network finds nothing, test again from mobile data or a VPN, because some malware also filters by IP address. Google Search Console's URL Inspection tool, with its live test, shows what Googlebot actually receives.

Where the code hides

These are the places I check, roughly in the order I find infections:

  1. .htaccess. Rewrite rules that test the user agent and referrer. They're only a few lines and easy to miss in a long file.
  2. Theme files. functions.php, header.php and footer.php are favourites, often with obfuscated code using eval, base64_decode or gzinflate.
  3. The database. Scripts inside post content, widgets or options, including header and footer injection settings. File scans never see these.
  4. Must-use plugins and fake plugins. A file in wp-content/mu-plugins loads on every request without appearing in the plugin list.
  5. JavaScript files. A line appended to a legitimate file, including jQuery, which is loaded on nearly every page.
  6. Tag managers and third-party scripts. An attacker with admin access can add a script through a tracking or header plugin, so it never touches your files.
  7. Cache layers. A caching plugin or CDN can keep serving an infected copy of a page after the source was cleaned.

A typical malicious rule in .htaccess looks like this (the destination is a placeholder):

RewriteCond %{HTTP_USER_AGENT} (android|iphone|mobile) [NC]
RewriteCond %{HTTP_REFERER} (google|bing|yahoo) [NC]
RewriteRule ^(.*)$ https://spam.example/ [R=302,L]

Conditions based on the browser and the referrer, followed by a redirect, are the signature. A legitimate WordPress rewrite block doesn't contain either.

Commands that save hours

With SSH and WP-CLI, a few searches cover most of the list above:

# Core and plugin files that differ from the official versions
wp core verify-checksums
wp plugin verify-checksums --all

# Obfuscated PHP anywhere under wp-content
grep -rIl --include="*.php" -E "eval\(base64_decode|gzinflate\(|str_rot13\(" wp-content

# Recently modified PHP and JS files
find . -type f \( -name "*.php" -o -name "*.js" \) -mtime -14 -not -path "./node_modules/*"

# Suspicious strings inside the database
wp db search "document.write" --all-tables --stats

# Administrators and scheduled tasks you didn't create
wp user list --role=administrator
wp cron event list

A hit isn't proof by itself, since some legitimate plugins use base64_decode, so check each result in context. Anything inside wp-content/uploads that ends in .php is a different story: that folder is for images and documents, so code there is almost always malicious.

After you remove it

  1. Purge every cache: the caching plugin, the server cache and the CDN.
  2. Run the same curl commands again and confirm both responses now match.
  3. Check Search Console under Security issues, and look at the Pages report for URLs you never published.
  4. Change every credential and update the plugin or theme that let them in.
  5. Test again after a week. If the redirect returns, something is still re-injecting it, usually a backdoor you haven't found.

When you can't find it

If you've reproduced the redirect and searched everything above without finding the source, stop editing files. A redirect that keeps coming back after you've deleted the obvious code means there's a second piece, a loader or backdoor, putting it back. At that point more manual searching tends to waste time. If you'd like me to take a look, the WordPress malware removal page explains how I work, and you can send me the URL and what you're seeing.

Redirect hacks on a single site are covered by my fixed $199 cleanup, including tracking down the loader that keeps putting the redirect back. You pay online, I email you within 4 hours for access, and it's done within 48 hours of getting in.

🎁 Free download

Get the free WordPress Security Checklist 2026

25-point checklist PDF - malware detection, hardening guide, login security. Used by 500+ WordPress site owners.

  • ✓ 25-point security checklist PDF
  • ✓ WordPress malware scan guide
  • ✓ Hardening checklist for any WordPress site

No spam. Unsubscribe any time.

Need help with your WordPress site?

I'm a freelance WordPress developer who fixes exactly this kind of problem.

150+ projects. Clients in UK, US, UAE & Ireland. Fast turnaround.

Rajan Gupta

Rajan Gupta

Freelance WordPress Developer

I'm Rajan Gupta, a freelance WordPress developer based in India with 150+ projects delivered for agencies and businesses in the UK, US, UAE, and Ireland. I specialise in performance optimisation, Core Web Vitals, custom Sage/ACF builds, and WooCommerce development. I target 90+ mobile PageSpeed where technically achievable, with a before-and-after report.

Related Articles