---
title: "WordPress Redirects to Spam Only on Mobile or Google"
description: "Your site looks fine to you but redirects visitors from Google or phones. Here is how to reproduce the conditional redirect and find where the code hides."
url: "https://rajangupta.com/wordpress-redirecting-to-spam-only-on-mobile-or-from-google-how-to-find-it/"
agent:
  instructions: "Use this search API to find relevant pages on rajangupta.com with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown. Any page is also available as Markdown by adding .md to its URL."
  endpoint: "https://rajangupta.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://rajangupta.com/llms.txt"
---

1. [Home](https://rajangupta.com/) /
2. [WordPress Security](https://rajangupta.com/blog/) /
3. WordPress Redirecting to Spam Only on Mobile or From Google? How to Find It

WordPress Security

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

December 1, 2026

By [Rajan Gupta](https://rajangupta.com/about/)

• ⏱ 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](https://rajangupta.com/wordpress-audit-tool/) is working exactly as designed: it only fires for people who aren't you.

## Table of Contents

1. Why you can't see it
2. Reproduce it from the command line
3. Where the code hides
4. Commands that save hours
5. After you remove it
6. When you can't find it
7. Related reading

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](https://rajangupta.com/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](https://rajangupta.com/wordpress-malware-removal/#pricing)** , 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.

## Related reading

- [WordPress hacked and redirecting visitors: the full removal process](https://rajangupta.com/wordpress-site-hacked-redirect-fix/)
- [I built a WordPress scanner that finds malware in 10 seconds](https://rajangupta.com/i-built-a-wordpress-scanner-that-finds-malware-in-10-seconds/)

🎁 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.

🎉

You're in!

Check your inbox - the checklist PDF is on its way.

Need help with your WordPress site?

I'm a [freelance WordPress developer](https://rajangupta.com/) who fixes exactly this kind of problem.

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

[Hire me](https://rajangupta.com/contact-us/) [Book a free call](https://cal.com/rajangupta/discovery?user=rajangupta&overlayCalendar=true)

![Rajan Gupta](https://rajangupta.com/wp-content/uploads/2026/01/freelance-wordpress-developer-rajan-gupta-150x150.webp)

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

- [WordPress Malware Removal Cost in 2026: What You Should Actually Pay](https://rajangupta.com/wordpress-malware-removal-cost-in-2026-what-you-should-actually-pay/)
- [WordPress Site Hacked? What to Do in the First Hour](https://rajangupta.com/wordpress-site-hacked-what-to-do-in-the-first-hour/)
- [Modern WordPress Security Stack for 2026: Zero‑Trust, 2FA, WAF, and Secure Headers](https://rajangupta.com/modern-wordpress-security-stack-for-2026-zero-trust-2fa-waf-and-secure-headers/)
- [How to Tell if Your WordPress Site Has Been Hacked (15 Signs)](https://rajangupta.com/how-to-know-if-your-wordpress-site-has-been-hacked/)

Need a WordPress fix?

### I fix exactly this kind of problem.

150+ projects. 4.7★ on Google. Clients in UK, US & UAE. Fast turnaround.

- ✔ Performance-focused builds
- ✔ Clean UI + strong UX
- ✔ SEO-ready structure

[Book a free call](https://cal.com/rajangupta/discovery?user=rajangupta&overlayCalendar=true) [Work with me](https://rajangupta.com/contact-us/)

No spam. No pressure.

#### Free Security Checklist PDF

25 hardening steps. Takes 5 minutes to run.

No spam. Unsubscribe any time.

✓ You're in!

First tip coming soon.

Security tool

### Check your WordPress site

Scan for malware, risky code, and security issues in seconds.

- ✔ Malware detection
- ✔ Security checks

[Visit WP Scan](https://wp-scan.org/) [Run Malware Check](https://wp-scan.org/malware-check)

---

Site index for AI agents: https://rajangupta.com/llms.txt
Contact: https://rajangupta.com/contact-us/


<!-- Page cached by LiteSpeed Cache 7.9.4 on 2026-12-01 18:03:27 -->
<!-- Guest Mode -->