Why is my WordPress site redirecting to a spam site?
How to confirm a WordPress redirect hack safely, rule out simple causes like changed site addresses or a lapsed domain, lock the site down and find where the redirect code is hiding.
A customer tells you your website sent them to a "you've won" page, a pharmacy shop, an adult site or a box asking them to prove they're human. You check on your laptop and everything looks normal.
That pattern is the giveaway. Most redirects like this are malware added to the site, and it's written to hide from the owner. It often skips logged-in users, only fires once per visitor or only targets phones and people arriving from Google search.12
Not every redirect is a hack, though. A changed site address, a stray redirect rule, a lapsed domain or a bad advert can look similar. This guide covers how to confirm what's happening, rule out the simple causes, lock things down and find where the redirect lives. For the full clean-up process, see our guide to cleaning up a hacked WordPress site.
See it the way your visitors do
Before changing anything, see the redirect for yourself. Hacked redirects can depend on where the visitor came from, their browser or their device,1 and samples analysed by Sucuri skipped logged-in users and administrators.23 So test the way a stranger would:
- Log out of WordPress and open a private or incognito window.
- Use your phone with Wi-Fi switched off, so you're on mobile data.
- Search Google for your business name and tap your own result, rather than typing the address.
- Try a few pages, not only the home page, with a fresh private window each time, since some redirects only fire once per visitor.
Note the address you landed on, the device, how you got there and the time. WordPress's documentation recommends keeping notes like this from the start.4 Don't interact with the page you land on. If it asks you to allow notifications, install something or paste a command to prove you're human, close the tab. Use a phone or computer with its updates installed and close the page as soon as you see where it went. Google advises against browsing infected pages,5 so if you'd rather not risk it, rely on the scanners below and the Security issues report in Search Console.
Free scanners such as Sucuri SiteCheck load your pages the way a visitor would and look for malware and suspicious redirects, and I keep a list of other free malware and blocklist checkers. A clean result doesn't prove much, because the scanner may not look like a phone arriving from Google.
Rule out the simple causes
A few problems that aren't malware can also send visitors elsewhere. They usually behave the same way for everyone, every time.
| What you see | Likely cause |
|---|---|
| Everyone is sent away, including you when logged in, and the dashboard goes there too | Site addresses changed |
| One or two specific pages always go to the same place | A redirect rule |
| The whole domain shows a parking page, adverts or someone else's site | Domain expired or DNS changed |
| The page loads, then jumps away a moment later, but not every time | An advert or third-party script, or injected code |
| Only some visitors, often on mobile, from Google or logged out | Almost certainly malware |
Your site addresses
Under Settings→ General, check the WordPress Address (URL) and Site Address (URL).6 They're stored as siteurl and home in the options table, usually wp_options (your table prefix may differ),7 and if either points at another domain, WordPress sends everyone there. If the fields are greyed out, they're being set by WP_SITEURL and WP_HOME lines in wp-config.php, which override the database,86 so check those lines too.
If the dashboard redirects as well, our guide to fixing mismatched site addresses shows how to set the right address in wp-config.php. Download a copy of the file first, since one typo can take the site offline. If nobody with access to the site changed the address, treat it as a hack.
A redirect rule
If you use the Redirection plugin, check its rules under Tools→ Redirection.9 Some SEO plugins have their own redirect managers, and your hosting control panel or CDN may have redirect settings too. A rule with a typo is a mistake. A rule nobody added that points to a spam domain is a hack. If you're stuck in a loop rather than sent elsewhere, see fixing WordPress redirect loops.
Your domain or DNS
If the whole domain shows a parking page, an expiry notice or another site entirely, even at /wp-admin/, the problem is probably the domain rather than WordPress. Check the renewal date in your registrar's account and ask the registrar to confirm where your DNS points. Our guide to a WordPress site that's down has more on this.
An advert or third-party script
Adverts and scripts loaded from other companies' servers run on your pages with the same power as your own code. Adverts can redirect visitors too. In 2024, the polyfill.io script service changed hands and was used to send some mobile visitors to a sports betting site, on more than 100,000 websites that loaded it.10 If you run adverts, report the redirect to the ad network and switch ads off for a day to test. Also check your theme settings and any header and footer plugin for scripts loading from domains you don't recognise.
Contain it before you clean
Once you're fairly sure it's a hack, stop further harm and lock down your logins before you go hunting for code.
Take a full backup of the files and database as they are now, store it away from the server and label it as infected. WordPress's documentation recommends this snapshot because it shows what changed and gives you a fallback.4
Then tell your host. They can check the server logs, and on shared hosting a hack can affect more than one site.4 Ask them to take the site offline or restrict access while you clean it. A maintenance mode plugin only works inside WordPress, so redirect rules in .htaccess would still fire.
Change every password connected to the site: WordPress administrators and editors, the hosting control panel, SFTP and FTP accounts and the database user.4 If you change the database password, update DB_PASSWORD in wp-config.php to match (with a copy of the file saved first), or you'll get a database connection error.
Next, log everyone out, attacker included, by replacing the eight keys and salts in wp-config.php (AUTH_KEY, SECURE_AUTH_KEY and so on). WordPress uses them to secure login cookies, and changing them forces everyone to log in again.11
- Download a copy of
wp-config.php. - Get eight fresh lines from the WordPress.org secret-key generator.
- Paste them over the eight existing key and salt lines, then save.
A few plugins, such as Google's Site Kit, use these keys to encrypt data they store, so you may need to reconnect them afterwards.12 New keys don't cancel application passwords, so open each administrator's profile under Users→ All Users and revoke any entries under Application Passwords that you don't recognise.
None of this removes the redirect code or a hidden backdoor. It shuts the obvious doors while you look.
Check where redirect code hides
Redirect malware tends to sit in the same handful of places. Look for anything you didn't add, especially code mentioning the domain you were sent to, long strings of scrambled text, eval(, base64_decode( or window.location. Some legitimate plugins use these too, so compare with a clean copy before deleting anything.
.htaccess
On Apache and LiteSpeed servers, .htaccess runs before WordPress loads. WordPress's documentation calls it one of the most commonly modified files in a hack, and copies can turn up in subfolders too.4 A standard single site only needs this block:13
# BEGIN WordPress
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]
# END WordPress
The version WordPress writes also has <IfModule mod_rewrite.c> lines around the rules and a comment, which are normal. Some hosts also set your PHP version in this file, so check with them before renaming it.
Be suspicious of rules mentioning HTTP_REFERER, HTTP_USER_AGENT, google or iphone that send visitors to another domain. If you're unsure, take a backup, rename the file to .htaccess_old, then go to Settings→ Permalinks and click Save Changes to create a fresh one. Copy back any rules you trust (from your host, caching or security plugin) one block at a time. Nginx servers don't use .htaccess, so ask your host to check the server's redirects instead.
Theme, plugin and snippet code
Injected code often lands in your theme's header.php, footer.php or functions.php, or the main index.php, since those run on every page.42 Check the settings of any code snippets or header and footer plugin as well, for <script> tags you didn't add.
If you have WP-CLI, wp core verify-checksums and wp plugin verify-checksums --all list WordPress and WordPress.org plugin files that have been changed or added.1415 Compare premium plugins with a fresh download from the developer.
The database
Redirects can also live in the database, in post and page content, in widgets (which are stored in the options table)16 and in other options that plugins and themes print into your pages. Back up the database first, then search for <script and the spam domain with the Search tab in phpMyAdmin or WP-CLI's wp db search.17 Hand-editing the database is easy to get wrong, and our guide to cleaning the database after a hack covers it with more care.
Users, must-use plugins and uploads
Under Users→ All Users, click the Administrator filter and look for accounts you don't recognise. Sucuri advises checking for administrator accounts nobody authorised, since an extra admin gives an attacker a way back in.3 Take a backup before deleting a user, and check what the account posted first, since WordPress asks whether to delete its content or attribute it to another user.18
wp-content/mu-plugins holds must-use plugins. WordPress runs every PHP file placed directly in it automatically, and they can't be deactivated from the dashboard.1920 Code can hide them from the Plugins screen, so look at the folder itself over SFTP. Sucuri has found redirect malware here that was written to skip administrators.3 Some hosts keep their own files in this folder, so ask yours before removing anything you don't recognise.
Look in wp-content/plugins for folders you didn't install, too. Fake plugins often have plausible names, and code can hide plugins from Plugins→ Installed Plugins,20 so compare the folders with what the dashboard shows. And wp-content/uploads should only hold media, so a .php file there is a strong sign of a backdoor, so take a backup and get a second opinion if you're unsure before deleting it.
Clean up and close the way in
If you have a backup from before the redirect started (before the first report, not just before you noticed), restoring it is usually quickest. You'll lose anything added since, such as orders and form entries, so check with your host or a developer before restoring a shop. A restore doesn't close the way in, so the rest of this section still applies.
Otherwise, replace the code with fresh copies, taking a backup first. Taking a backup first, delete the wp-admin and wp-includes folders and upload fresh copies from the same WordPress version you're running, rather than uploading over the top or using the dashboard's re-install option. Both only overwrite existing files and leave an attacker's extra ones behind.4 Replace the WordPress files in the main folder too, apart from wp-config.php. Don't replace wp-content or wp-config.php, but do check wp-config.php for code you don't recognise.
Replace each plugin and theme you use with a fresh copy from WordPress.org or the developer. Swap the folder over SFTP, or upload the zip under Plugins→ Add Plugin (Add New Plugin on older versions) and choose Replace current with uploaded, rather than deleting it in the dashboard, which can erase its settings. Remove any you don't use and never reinstall a nulled (pirated) one. Compare a customised theme with a clean copy rather than overwriting it. Then update everything to the latest versions.
Next, work out how they got in, or it will happen again. The usual suspects are an outdated plugin or theme with a known security hole, a nulled plugin or a weak or reused password. Our guides to plugin security vulnerabilities and closed or abandoned plugins cover the first of those, and your host can search the access logs from around when the redirect started.
Finally, repeat the tests from the start of this guide and change the passwords again.4
Clear Google and browser warnings
If Google spotted the hack, your site may carry a warning in search results, and browsers can show a full-page warning before visitors reach it.21 You can check your domain on Google's Safe Browsing site status page.
If you use Google Search Console, open the Security issues report. It lists what Google found, with example pages.5 Once every listed problem is fixed, request a review from that report and explain what you found and what you changed. Don't send another request while one is still pending.5
Google says reviews take anything from about a day for phishing to several weeks, with malware reviews taking a few days or longer.215 Once the site is found clean, browser and search warnings are removed within 72 hours.21 If you haven't set up Search Console yet, our guide to getting your site on Google walks through it. Bing and antivirus products keep their own blocklists too, so a warning from one of those needs raising with them separately.4
Stop it coming back
Redirect hacks often come back a few days after they seem to be fixed. That's almost always because a backdoor was missed or the way in is still open, and the attacker simply puts the redirect back. If you've cleaned it once and it's returned, this is a good point to call in help rather than going round again.
To make a repeat less likely:
- Keep WordPress, plugins and themes updated, and remove anything you don't use.
- Use strong, unique passwords and two-factor authentication on admin accounts.
- Give admin access only to people who need it, and remove old accounts.
- Keep regular backups stored away from the server, so you always have a clean copy to go back to.
- Run regular malware scans and turn on file change alerts in a security plugin.
WordPress's hardening guide covers further measures,22 and our guide to dealing with WordPress spam covers protecting your login page.
Common questions
Why can't I see the redirect when my customers can?
Because the malware is usually built to hide from you. Many redirect infections are written to skip logged-in users and fire only for some visitors.12 Log out, open a private window on your phone using mobile data, then search for your business on Google and tap your own result. That's the closest you'll get to what a customer sees.
Is it my host's fault?
Usually not. In my experience most redirect hacks come in through an outdated plugin or theme, a nulled plugin or a weak or stolen password, which sit with whoever manages the site. On shared hosting a hack can sometimes affect other sites on the same server, so tell your host straight away.4 They can check logs, scan the account and help restore a backup.
Will restoring a backup lose my orders or posts?
Yes, anything added after the backup was taken will be lost, including orders, form entries, comments and posts. For a shop, note down or export recent orders first, or ask your host or a developer whether clean files can be combined with your current database once it's been checked. A restore also doesn't close the way in, so update everything afterwards.
Why did the redirect come back after I removed it?
Because something was left behind. Attackers usually leave a backdoor, such as a fake plugin, a must-use plugin, a PHP file in uploads or a hidden admin account, and use it to put the redirect back. If the security hole they first used is still open, they can also come straight back in the same way. Finding both is the hard part of any clean-up.
Still being redirected?
If the redirect keeps coming back, or you'd rather not dig through files and databases yourself, my emergency WordPress support covers finding the redirect, removing it and closing the way in. Afterwards, a maintenance plan keeps updates, backups and security checks ticking over.
Spam policies for Google web search, Google Search Central. ↩ ↩ ↩
Malware Redirects WordPress Traffic to Harmful Sites, Sucuri Blog, 23 January 2025. ↩ ↩ ↩ ↩
Hidden Malware Strikes Again: Mu-Plugins Under Attack, Sucuri Blog, 28 March 2025. ↩ ↩ ↩
FAQ My site was hacked, WordPress.org Documentation. ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩ ↩
Security issues report, Search Console Help. ↩ ↩ ↩ ↩
options-general.php, WordPress source code on GitHub. ↩ ↩
schema.php (populate_options), WordPress source code on GitHub. ↩
wp-config.php, WordPress Advanced Administration Handbook. ↩
redirection-admin.php, Redirection plugin source code on GitHub. ↩
Polyfill supply chain attack hits 100K+ sites, Sansec, 25 June 2024. ↩
wp-config-sample.php, WordPress source code on GitHub. ↩
Data_Encryption.php, Site Kit by Google source code on GitHub. ↩
Apache HTTPD / .htaccess, WordPress Advanced Administration Handbook. ↩
wp core verify-checksums, WP-CLI Commands. ↩
wp plugin verify-checksums, WP-CLI Commands. ↩
class-wp-widget.php, WordPress source code on GitHub. ↩
wp db search, WP-CLI Commands. ↩
load.php (wp_get_mu_plugins), WordPress source code on GitHub. ↩
class-wp-plugins-list-table.php, WordPress source code on GitHub. ↩ ↩
Request a review, web.dev. ↩ ↩ ↩
Hardening WordPress, WordPress Advanced Administration Handbook. ↩