Skip to content

WordPress hacked redirect: how to detect and remove spam redirects

· The AYT team · 2 min read

Man typing on a laptop at a wooden table, with a phone and notebook beside it

A spam redirect means someone added code to your WordPress site that sends visitors to another site (gambling, pharmacies, ads, "you've won a phone"). The code usually hides in the database (the site's options or post content), in the theme's files, in .htaccess or in a fake plugin. To remove it, first confirm the redirect, then search those places in order, and finally close the hole it came through.

Why you don't see the hacked redirect

The code is written so the owner won't notice. The usual variations:

  • it redirects only visitors on a phone;
  • only those coming from Google or social networks (based on the Referer header);
  • only on the first visit, after which it sets a cookie and leaves you alone;
  • never anyone signed in to wp-admin.

So a customer tells you the site "goes to a casino", while you, on your computer and signed in, see the normal site.

Step 1: confirm the redirect

Try these in turn:

  1. Open the site on your phone, on mobile data, in a private window.
  2. Search for the site in Google and click the result, again from a private window.
  3. Simulate a mobile visitor arriving from Google, from Terminal:
curl -sI -A "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)" -e "https://www.google.com/" https://site.com/

If the response contains location: with a foreign domain, you've confirmed the redirect. If the response is 200, the redirect may happen in JavaScript, in the browser. Then run the command without -I, save the page and look in it for <script tags with unknown addresses.

  1. Use an external scanner such as Sucuri SiteCheck, and the URL Inspection tool in Google Search Console, which shows you the page as Google sees it.

Write down the domain the redirect goes to. You'll search for it in the files and the database.

Step 2: check the site address in the database

If the redirect affects everyone, including you in wp-admin, first check the siteurl and home options in the wp_options table (in phpMyAdmin). An attacker who changes them sends the whole site to their domain. Correct them directly in the table.

Step 3: search the database for scripts

Most often the code is injected into post content or into options. In phpMyAdmin, run searches like these (replace wp_ with your table prefix):

SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%<script%';
SELECT option_name FROM wp_options WHERE option_value LIKE '%<script%';
SELECT option_name FROM wp_options WHERE option_value LIKE '%spam-domain%';

Not every <script> is malicious: some plugins and analytics snippets store legitimate scripts. The suspicious ones load files from unknown domains or contain scrambled, unreadable code with eval, atob or String.fromCharCode. To remove them, use a search and replace tool that understands serialized data, not a plain REPLACE, so you don't break plugin settings.

phpMyAdmin opened from the panel, showing a WordPress database's tables

Step 4: check the theme and WordPress files

The favourite places:

  • the active theme's header.php, footer.php and functions.php (code added at the start or end of the file);
  • index.php, wp-config.php and wp-blog-header.php in the site's root;
  • wp-content/mu-plugins/, where plugins run automatically and don't appear in the wp-admin list;
  • plugins with plausible names that you didn't install;
  • .php files in wp-content/uploads/, where there shouldn't be any.

Replace the WordPress core files with a clean copy of the same version instead of repairing them by hand. Reinstall the plugins and the theme from their official source.

Step 5: .htaccess (on Apache)

On Apache servers, the redirect can be just a rule in .htaccess that checks whether the visitor came from Google:

RewriteCond %{HTTP_REFERER} (google|bing|facebook) [NC]
RewriteRule .* https://spam-domain.example/ [R=302,L]

Look for rules like this in .htaccess in the root and in subfolders. The normal WordPress rules are only the ones between # BEGIN WordPress and # END WordPress. nginx doesn't read .htaccess, so there it has no effect.

Step 6: clean up fully and close the hole

The redirect is only the symptom: whoever added it has access to the site and has usually left a backdoor too. Once you've removed it:

  • change the passwords (wp-admin, hosting, FTP, database) and the security keys in wp-config.php;
  • delete administrators you don't recognise;
  • update WordPress, the plugins and the theme, and delete what you don't use;
  • scan the whole site again.

The full steps, including restoring from backup and requesting a Google review, are in the guide to cleaning up a hacked WordPress site.

Frequently asked questions

I removed the code, but some visitors are still redirected. Why?

A 301 redirect stays in the browser's memory. Visitors who received it once may keep being sent to the spam site until they clear their cache. Test from another browser or a private window. If it still happens there, the code isn't fully gone.

Why did the redirect come back after a few days?

A backdoor is still there (a hidden PHP file, an unknown administrator, a vulnerable plugin) and it rewrites the code. Without step 6, the cleanup only lasts until the attacker's next visit.

On AYT hosting

In the AYT panel, the Virus Scanner checks the account's files, and the WordPress Toolkit's security checklist shows what still needs hardening. The WordPress Toolkit's database search and replace and phpMyAdmin, opened from the panel, help with steps 2 and 3. On paid plans, malware cleanup is included, and backups kept for up to 24 months give you a copy from before the infection. The web application firewall (ModSecurity) and login rate limiting reduce the odds of another attack. The details are on the WordPress hosting page.

More articles

All articles