WordPress malware

WordPress redirects to other websites – causes and fixes

WordPress redirects to other websites – causes and fixes

If WordPress redirects visitors to other websites, treat it as a serious warning sign. Sometimes the cause is a configuration mistake, broken plugin or cache issue, but in real-world incidents it is often malware. A visitor opens your site and ends up on a pharmacy, casino, crypto, adult or scam page. Sometimes it happens only on mobile. Sometimes only from Google search. Sometimes only once per day.

Redirect malware is dangerous because the owner often does not see it. Logged-in admin traffic may look normal. Desktop visits may look normal. But real visitors are already being lost. This can cause revenue loss, reputation damage, Google Ads suspension and SEO problems.

This article explains how WordPress redirect malware works, where to look for it and how to remove it in a way that does not lead to reinfection.

Typical symptoms

Redirect infections often look like this:

If your advertising is affected, read the related article about Google Ads malicious software suspensions, because the review process should only start after the site is clean.

How redirect malware works

The attacker wants to send part of your traffic somewhere else. That can be done server-side in PHP, client-side in JavaScript, through database content, via .htaccess rules or by loading an external script.

Common methods include:

A simple JavaScript redirect can look like this:

<script>
  if (document.referrer.includes('google')) {
    window.location.href = 'https://spam-domain.example';
  }
</script>

A PHP-based redirect can look like this:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false) {
    header('Location: https://suspicious-domain.example');
    exit;
}

Real malware is usually more obfuscated, but the logic is similar: detect the right visitor, then redirect.

Where to search first

Start with the areas that attackers commonly modify:

.htaccess
wp-config.php
wp-content/themes/*/functions.php
wp-content/plugins/*/*.php
wp-content/uploads/**/*.php
wp-content/mu-plugins/*.php
wp-includes/js/

Also inspect the database. Redirect scripts are often inserted into posts, widgets, theme options, plugin settings or the wp_options table.

Useful search terms include:

window.location
document.location
location.href
eval(
base64_decode
fromCharCode
iframe
onload

But do not rely only on string search. Modern malware can load the redirect from an external source, split payloads into several fragments or activate only under specific request conditions.

Why you may not see the redirect

Redirect malware often tries to hide from admins and developers. It may not run when:

That is why testing should include incognito windows, mobile devices, clean browser profiles, different networks and simulated referrers. HTTP logs can also reveal redirect chains that are hard to reproduce manually.

Cleaning the redirect is not enough

Removing the visible redirect is only the first step. If the attacker still has a backdoor or stolen credential, the site can be modified again.

A durable cleanup should include:

If a security plugin says the site is clean but redirects still happen, see Wordfence says the site is clean, but it is still infected.

Prevention after cleanup

Prevention is about reducing the chance of the same incident returning:

Backups matter, but backups alone are not security. If the vulnerability or leaked password remains, restoring the site simply restores the next infection target.

When to act immediately

Treat the incident as urgent if redirects affect paid traffic, checkout pages, lead forms, customer portals or high-ranking SEO pages. Redirect malware damages trust quickly and can spread into advertising, analytics and search visibility.

The right fix is not only to stop the redirect. It is to find why it was possible, close the entry point and monitor the site afterwards.

Want to avoid the next WordPress infection?

WebShield helps with continuous protection, backups and logging so reinfections are easier to prevent.