WordPress Hacked Redirect: Why Your Site Redirects to Spam and How to Remove the Malware
WordPress hacked redirect sending visitors to spam? Catch the hidden redirect, find the malware in .htaccess, files or the database and stop it returning.

On this page
- My WordPress website is redirecting: what to do in the first hour
- First, make sure it's really a hack
- Why a hacked WordPress redirect hides from you
- How to catch the redirect
- Where WordPress redirect malware hides
- How to fix the WordPress redirect hack, step by step
- Why the redirect keeps coming back
- Commands that find WordPress redirect malware
- When to get help
- FAQs
- A WordPress hacked redirect is almost always malware that targets mobile visitors from Google while hiding from logged-in admins, so test the way a real visitor arrives.
- Redirect malware hides in .htaccess, wp-config.php, index.php, theme and JavaScript files, the database, fake plugins, mu-plugins and cron jobs, so check every location rather than trusting one scan.
- Removing the visible code is not enough: replace core, plugins and themes, remove backdoors, change every password, purge every cache and close the entry point, or the redirect comes back.
A WordPress hacked redirect happens when malware injected into your site sends some visitors to spam pages (gambling, dating, fake prizes or "your phone has a virus" warnings) instead of your content. If your WordPress site is redirecting to spam and you didn't set the redirect up, assume redirect malware: the code usually targets mobile visitors arriving from Google while hiding from logged-in admins, which is why the site looks normal when you check it yourself.
Short answer: To fix a WordPress hacked redirect, confirm it on a phone from a Google result, take a backup and change every password. Then clean .htaccess, wp-config.php, theme files, the database and rogue plugins or cron jobs, reinstall core and plugins from clean copies, purge every cache and patch the hole the attacker used.
My WordPress website is redirecting: what to do in the first hour
- Don't panic-delete files. Take a full backup of the infected site first; it is evidence and a fallback if a clean-up step breaks something.
- Write down what you see: the spam domains, which devices and browsers are affected, and when it started.
- Change your hosting and WordPress admin passwords from a device you trust, so the attacker loses easy access while you work.
- Pause paid ads pointing at the site, so you are not paying to send people to spam.
- Check Search Console for a Security issues warning, so you know whether Google has already flagged the site.
Then work through the rest of this guide in order.
First, make sure it's really a hack
A few harmless things can look like a redirect hack. Rule them out quickly:
- A redirect rule you or your developer added. Check any redirect plugin and your SEO plugin's redirect settings.
- "Too many redirects" errors are a settings loop, not spam; see fixing ERR_TOO_MANY_REDIRECTS.
- Ads or third-party scripts. A bad ad or a compromised widget script can redirect visitors without your site files being hacked.
- Only one person sees it. Adware or a browser extension on their own device may be the cause. Ask whether it happens on other devices.
- Domain or DNS problems. An expired domain or changed DNS records can send all visitors to someone else's server.
If it happens to different people on different devices and you didn't set it up, treat it as malware. For the wider warning signs, see how to know if your WordPress site has been hacked.
Why a hacked WordPress redirect hides from you
Redirect malware is usually conditional. The code checks who is visiting before deciding whether to redirect:
- Device: it reads the user agent and targets phones, because owners mostly check on a computer
- Referrer: it redirects people arriving from Google, Facebook or other search engines, but not people who type your address
- Logged-in status: it looks for the WordPress login cookie and leaves administrators alone
- Once per visitor: it sets a cookie so the second visit looks normal
- Location or IP: it targets certain countries, or skips IP addresses that have used the dashboard
That is also why one quick check from your office computer proves nothing: test the way a real visitor arrives, on a phone, from a search result, logged out and without old cookies.
Caching adds another layer. Logged-out visitors get cached pages while admins bypass the cache, so an infected cached page can keep redirecting visitors after the code is removed.
How to catch the redirect
- Phone test: switch off Wi-Fi, open a private tab, search Google for your business and tap your result. Use a fresh private tab for each attempt.
- Desktop test: in Chrome DevTools, turn on device emulation, open the Network tab, tick "Preserve log", then visit your site from a Google result and watch for unfamiliar domains.
- View the page source: search for script tags loading from unfamiliar domains, and for scrambled code containing things like
eval(,atob(orString.fromCharCode. - Remote scanner: an online scanner fetches your pages as a visitor would and flags known redirect code; see WordPress malware scanners compared.
- Search Console: check the Security issues report for hacked content or malware warnings.
If you're comfortable with a terminal, you can pretend to be a mobile visitor arriving from Google:
curl -sI -A "Mozilla/5.0 (iPhone)" -e "https://www.google.com/" https://yourdomain.com/
A Location: header pointing to another domain means a server-side redirect. JavaScript redirects only run in a browser, so use the browser tests as well.
What the symptom tells you
| Symptom | Most likely location |
|---|---|
| curl shows a Location header to a spam domain | .htaccess, wp-config.php, index.php or a PHP file that loads early |
| Page loads, then jumps to another site | Injected JavaScript in theme files, .js files or the database |
| Only visitors from Google on mobile are redirected | Conditional rules checking referrer and user agent, often in .htaccess or theme PHP |
| Every page, including wp-admin, goes elsewhere | Changed siteurl or home options in the database, or DNS |
| Redirect returns hours after cleaning | A backdoor, a cron job or another infected site in the same hosting account |
Where WordPress redirect malware hides
.htaccess files
On Apache and LiteSpeed hosting, .htaccess runs before WordPress loads. A malicious rule often looks like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} (google|bing|facebook) [NC]
RewriteCond %{HTTP_USER_AGENT} (android|iphone|mobile) [NC]
RewriteRule ^(.*)$ https://spam-domain.example/ [R=302,L]
That rule redirects only mobile visitors arriving from search or Facebook. Check .htaccess in your main folder, in subfolders such as wp-content and uploads, and in any folder above your site. Keep the standard # BEGIN WordPress block and rules you can explain, such as those from your caching or security plugin.
wp-config.php and index.php
These files load on every request. Look for code before the normal opening <?php line, long encoded strings, or an include of a strange file disguised with an image or icon extension:
<?php @include "/home/account/public_html/wp-content/uploads/2024/03/.cache.ico"; ?>
The root index.php in a normal WordPress install is only a few lines long. If yours is much longer, compare it with a fresh download.
Theme and JavaScript files
Check header.php, footer.php and functions.php in your theme and child theme. Injected scripts are often disguised as jQuery, analytics or a CDN file:
<script src="https://cdn-jquery-min.example/jquery.min.js"></script>
Also look for PHP that reads HTTP_USER_AGENT or HTTP_REFERER, and for long scrambled lines appended to theme and plugin .js files. Sort by last modified date and compare suspicious files with clean copies.
The database
Redirect scripts are often injected into posts, widgets, theme options or "header and footer scripts" settings. Your table prefix may differ from wp_. In phpMyAdmin:
SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%<script%';
SELECT option_name FROM wp_options WHERE option_value LIKE '%<script%';
With WP-CLI you can search the whole database and check the site address:
wp db search "<script" --all-tables
wp option get siteurl
wp option get home
Some results will be legitimate embeds such as maps or booking widgets, so review each one. Back up the database before editing anything.
Rogue plugins, mu-plugins and cron jobs
Look in wp-content/plugins for folders you didn't install, especially with names that mimic real plugins; some malware hides itself from the Plugins screen, so compare the server folders with your dashboard list. Check wp-content/mu-plugins, which loads automatically and is easy to miss, and drop-ins such as advanced-cache.php that your caching setup didn't create. If the redirect returns within hours, look for a cron job in your hosting panel and list WordPress events with wp cron event list.
How to fix the WordPress redirect hack, step by step
- Back up the infected site (files and database) and note the redirect destinations and dates.
- Change every password: hosting, SFTP, database (then update wp-config.php) and all WordPress admins. Generate new security keys in wp-config.php to log everyone out.
- Remove admin users you don't recognise.
- Replace WordPress core: fresh wp-admin and wp-includes, then check root files with
wp core verify-checksums. - Reinstall plugins and themes from official sources, and delete unused or nulled ones.
- Clean .htaccess, wp-config.php and index.php.
- Remove PHP files from uploads and anything suspicious in mu-plugins.
- Clean the database: injected scripts, spam posts and changed site URLs.
- Remove malicious cron jobs and scheduled events.
- Purge every cache: plugin, server and CDN.
- Test again on mobile data, from Google, in fresh private windows, over the next few days.
Backdoors are what usually bring the redirect back, so read finding and removing WordPress backdoors. The complete process is in how to remove malware from WordPress step by step.
Why the redirect keeps coming back
- A backdoor was missed: attackers usually leave several ways back in
- The entry point is still open: an outdated plugin or theme, or a reused password
- Another site in the same hosting account is infected; see cleaning a whole hosting account
- A cron job reinstalls the code
- A computer with saved FTP passwords is infected, so new passwords get stolen too
Commands that find WordPress redirect malware
If your host gives you SSH and WP-CLI, these checks find most redirect malware in minutes. Run them from the WordPress folder after taking a backup:
wp core verify-checksumslists core files that differ from the official release.wp plugin verify-checksums --alldoes the same for plugins from WordPress.org.grep -rlE "eval[(]|base64_decode|gzinflate|str_rot13" wp-content --include=*.phpfinds files using common obfuscation. Some genuine plugins use these, so review each hit rather than deleting it.find . -name "*.php" -mtime -10lists PHP files changed in the last 10 days, including in uploads, where PHP should not exist.wp db query "SELECT ID FROM wp_posts WHERE post_content LIKE '%<script%'"finds scripts injected into posts; run the same check onwp_options(adjust the table prefix if yours differs).wp cron event listandwp user list --role=administratorshow scheduled jobs and admin accounts you did not create.
No SSH? Your hosting file manager and phpMyAdmin can run the same searches, just more slowly. Once the site is clean, request a review in Search Console; see fixing the "This site may be hacked" warning.
When to get help
Get help straight away if the redirect returns within days of cleaning, Google shows a "This site may be hacked" warning, or the site takes payments, because checkout pages may be affected too.
If your site is redirecting visitors right now and you need it stopped quickly, I can find the redirect code, remove the backdoors behind it, close the entry point and help with the Google review, for stores as well as business sites. See WordPress malware removal. You can also send the details through the contact form.
FAQs
Why is my WordPress site redirecting to spam?
If you didn't set up the redirect, malware has most likely been injected into .htaccess, a core or theme file, a plugin or the database. Less often it's a bad ad, a compromised third-party script, or a domain or DNS problem.
My WordPress website is redirecting to another site. Is it hacked?
If it happens for different people on different devices and you didn't add a redirect rule, almost certainly yes. Rule out a redirect plugin, a bad ad, a domain or DNS problem and adware on one device first.
Should I use a WordPress malicious redirect cleanup service?
If the redirect returned after cleaning, the site takes payments or Google has flagged it, a professional clean-up is usually quicker and safer.
Will a WordPress hacked redirect hurt my Google rankings?
It can, through browser warnings, visitors bouncing straight back and spam pages indexed under your domain. Rankings usually recover after a thorough clean-up and a successful review, though how long that takes varies.
How do I stop WordPress redirect malware coming back?
Remove every backdoor, not just the redirect code, then update or delete the plugin or theme the attacker used. Change hosting, SFTP, database and admin passwords, generate new salts in wp-config.php, and clean any other site in the same hosting account. File-change monitoring will warn you if anything reappears.
Why does my WordPress site redirect only on mobile?
Redirect malware usually checks the visitor's device and referrer, sending phone users who arrive from Google to spam while showing desktop visitors and logged-in admins the normal site. That is why owners rarely see it. Test on mobile data, logged out, from a search result.
Need help with your website?
I'm a freelance WordPress developer building fast, SEO-friendly websites since 2020. Tell me what you need and I'll reply with a plan and a fixed quote, usually within 24 hours.


