Revenue Driven For Clients

2,120,240,443

Why Is My Website Redirecting to Another Site? (Causes & Fix)

Your website is redirecting visitors to a spam, pharmacy, gambling or adult site. You’ve checked the link you shared — it looks fine. But when someone clicks on it from Google, they end up somewhere else entirely. Worse, it might only happen on mobile. Or only to visitors who’ve never been to your site before.

This is a redirect hack. It is one of the most common and most damaging forms of website malware, and it is almost never something you can fully fix by editing a single file. This guide explains exactly what is happening, where the malicious code hides, and how to remove it permanently.

In This Guide

What a redirect hack is and how it works
The 5 different types of redirect hacks and where they hide
Why your site might only redirect on mobile
Step-by-step: how to find and remove redirect malware
Why DIY fixes almost always fail
How to prevent a redirect hack from happening again

What Is a Website Redirect Hack?

A redirect hack (also called a redirect malware infection) occurs when an attacker injects malicious code into your website’s files or database. This code intercepts visitors’ requests and sends them to a different URL — one controlled by the attacker.

The destination sites are typically: pharmaceutical spam stores, online casino and gambling sites, adult content platforms, fake virus warning pages (designed to trick users into downloading malware), or phishing pages designed to steal login credentials or payment details.

Why Would a Hacker Do This?

Money. Redirect attacks are highly profitable for attackers. Affiliate traffic: The hacker earns commissions by sending your visitors to affiliate spam stores. Ad fraud: Your traffic is routed through pages loaded with ads, generating fraudulent ad revenue. Malware distribution: Your site becomes a vehicle to infect your visitors' devices. SEO manipulation: Your domain's authority is used to boost the rankings of spam sites.

The 5 Types of Redirect Hacks

Type 1: .htaccess Redirect Hack

The .htaccess file is an Apache server configuration file that controls how your site handles URLs. Attackers inject RewriteRule or RewriteCond directives that silently redirect visitors to another site. This is one of the easiest redirect hacks to find — but it is rarely the only infected file.

What malicious .htaccess code looks like:

RewriteEngine On
RewriteCond %{HTTP_REFERER} .*google.*
RewriteRule ^(.*)$ https://spam-pharmacy-site.com/ [R=302,L]

# Or a more obfuscated version:
RewriteRule .* http://redirect-destination.ru/ [L,R=301]

Type 2: Database Redirect Injection

Attackers inject redirect code directly into your WordPress database — typically the wp_options table (which stores site settings) or injected into post content. This type is particularly dangerous because scanning tools that only check files will completely miss it.

What to check in the database:

  • In phpMyAdmin: run a search in wp_options for ‘eval(‘ or ‘base64_decode’ in the option_value column
  • Search the wp_posts table for <script> tags or iframe tags you don’t recognise
  • Check the siteurl and home values in wp_options — attackers sometimes change these

Type 3: PHP File Injection (Core File Manipulation)

Attackers modify WordPress core PHP files — such as wp-config.php, wp-settings.php, wp-load.php, wp-blog-header.php — to add redirect code. Because these files load on every page request, the redirect triggers for every visitor. This code is often obfuscated with base64 encoding to make it hard to spot.

Example of obfuscated injection:

<?php
eval(base64_decode('aWYoaXNzZXQoJF9DT09LSUVbJ3Rlc3QnXSkpIHto...',
// This decodes to a redirect script — base64 is used to hide it from scanners
?>

Why This Is Dangerous

Even if you reinstall WordPress core files, attackers often also inject code into
theme files, plugin files, and the database simultaneously.
Reinstalling core alone will not fix the redirect.

Type 4: JavaScript Redirect Injection

Rather than a server-side redirect, attackers sometimes inject malicious JavaScript into your theme files (header.php, footer.php, functions.php) or directly into page content. This JavaScript runs in the visitor’s browser and redirects them client-side. This type is often conditional — it may only redirect visitors arriving from Google, only on mobile devices, or only on the first visit.

Example of injected JavaScript:

<script>
if(document.referrer.indexOf('google') >= 0){
  window.location='https://spam-destination.com';
}
</script>

Type 5: Plugin or Theme Backdoor Redirect

Nulled (pirated) themes and plugins frequently contain pre-installed redirect code. Additionally, attackers who have gained admin access will sometimes install a malicious plugin disguised as a legitimate tool (e.g., ‘WP Cache Pro’) that contains redirect functionality. These are particularly hard to spot because they appear to be legitimate WordPress plugins in your dashboard.

Is Your Website Redirecting Visitors? SEOHUB Will Fix It Fast.

Redirect hacks are one of the trickiest infections to fully remove. Our experts clean every trace
— including the backdoors that cause re-infection.

Why Does My Website Only Redirect on Mobile?

Mobile-only redirects are extremely common and are specifically designed to avoid detection. Here is why attackers use this technique:

  • Site owners typically check their site on desktop — so a mobile-only redirect goes unnoticed for longer.
  • Mobile visitors arriving from Google are more likely to assume they accidentally tapped the wrong thing, rather than report the issue.
  • It creates more profitable traffic — mobile users convert better on affiliate spam sites.
Redirect Condition What It Targets
Mobile user-agent only Visitors on smartphones — iPhone, Android
Google referrer only Visitors who clicked your site from Google search results
First visit only (cookie-based) New visitors — returning visitors see the site normally
Specific countries or IP ranges Visitors from high-value regions like US, UK, Australia
Non-logged-in users only Guests — logged-in admins see the site normally

How to Find Redirect Malware: Step-by-Step

Follow these steps in order to locate where the redirect code is hiding:

Step 1: Verify the Redirect Exists

  1. On mobile: Visit your site by clicking a Google search result for your own domain name in incognito mode.
  2. Check with Google: Use Google’s Mobile-Friendly Test tool at search.google.com/test/mobile-friendly.
  3. View source: Right-click your homepage and select View Source. Search for ‘window.location’, ‘document.referrer’, or ‘RewriteRule’.

Step 2: Check Your .htaccess File

  1. Connect via FTP (FileZilla) or your hosting File Manager in cPanel.
  2. Download your .htaccess file from the root directory.
  3. Open it in a text editor and look for any RewriteRule lines pointing to external URLs.
  4. Your .htaccess should only contain WordPress core directives unless you added customisations.
# A clean WordPress .htaccess looks like this:
# BEGIN WordPress
RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress # If you see lines OUTSIDE these tags pointing to external URLs — that is malware.

Step 3: Check WordPress Core Files

  1. Download wp-config.php, wp-settings.php, wp-load.php and wp-blog-header.php via FTP.
  2. Open each in a text editor and search for: eval(, base64_decode(, gzinflate(, str_rot13(, window.location
  3. Any of those functions in these files (except wp-config.php’s legitimate DB settings) is a strong indicator of infection.

Step 4: Check Theme Files

  1. In FTP, navigate to wp-content/themes/your-active-theme/
  2. Download header.php, footer.php, and functions.php
  3. Search for <script> tags, eval(), or base64_decode() that you did not add.

Step 5: Check the Database

  1. Log into phpMyAdmin from cPanel
  2. Select your WordPress database, then click SQL
  3. Run this query to find suspicious code in options: SELECT * FROM wp_options WHERE option_value LIKE ‘%eval(%’
  4. Run this to check posts: SELECT * FROM wp_posts WHERE post_content LIKE ‘%<script%’

Is Your Website Redirecting Visitors? SEOHUB Will Fix It Fast.

Redirect hacks are one of the trickiest infections to fully remove. Our experts clean every trace
— including the backdoors that cause re-infection.

Why DIY Fixes Almost Always Fail

Most site owners who discover a redirect hack try the following fixes — and most of these fail within 24 to 72 hours:
Common DIY Fix Why It Fails
Delete .htaccess and regenerate it The database injection and PHP file injections are still active. They will rewrite the .htaccess within hours.
Reinstall WordPress core Core files are replaced but the themes, plugins, uploads and the database still contain malware.
Restore from backup If the backup is from after the infection, the malware is already there. If before, the vulnerability is still present.
Delete infected plugin Backdoor shells in wp-content/uploads will reload the malware. The attacker still has server access.
Change admin password The attacker may already have FTP access, database access, or a backdoor shell — not just the WP admin.

The Root Cause Problem

Every redirect hack has a root cause — the vulnerability that let the attacker in.
This is almost always: an outdated plugin, a nulled theme, a weak password,
or a vulnerable hosting environment.
Cleaning the malware without fixing the root cause = re-infection within days.

Professional Redirect Hack Cleanup: What It Includes

A proper redirect hack cleanup by a professional security team covers all of the following — not just the files you can see:

  • Complete server-side file scan across all PHP files
  • Database scan and cleaning of all injected content
  • Backdoor shell identification and removal
  • Root cause analysis — finding the vulnerability that was exploited
  • Patch the entry point — update or replace the vulnerable plugin, theme or configuration
  • .htaccess restoration to a clean state
  • File permission hardening
  • Web Application Firewall installation
  • Google blacklist review submission if needed
  • Post-cleanup verification — confirmed clean before sign-off

Preventing a Redirect Hack: Your Security Checklist

Prevention Action Details
Keep all plugins and themes updated The #1 cause of redirect hacks is outdated plugins with known vulnerabilities. Enable auto-updates or check weekly.
Delete nulled/pirated themes and plugins These contain pre-installed malware by design. Replace with legitimate versions immediately.
Install a Web Application Firewall (WAF) A WAF blocks malicious requests before they reach your site. Essential for any live WordPress site.
Use strong, unique passwords for all accounts WordPress admin, FTP, database, cPanel, and email should all use different, strong passwords.
Enable two-factor authentication (2FA) Even if your password is stolen, attackers cannot log in without the second factor.
Run weekly automated malware scans Catch infections early before they cause lasting damage to rankings and reputation.
Take regular off-server backups Daily backups stored outside your server give you a clean restore point in a worst-case scenario.
Restrict file permissions Set file permissions to 644 and folders to 755. Never leave wp-config.php at 777.

Frequently Asked Questions

Can I fix a redirect hack by just editing my .htaccess file?

No. Editing .htaccess only removes one injection point. Redirect hacks plant code in multiple locations and always leave a backdoor. Without a full cleanup, the redirect returns within hours.

Why does the redirect only happen when coming from Google?

Attackers specifically target Google referrals because: (1) these visitors are real humans looking for something, (2) site owners rarely check their own site from Google, and (3) the infected site continues to appear normal to the owner while users are affected.

My site was restored from backup but the redirect came back. Why?

Either the backup was taken after the infection occurred, or the vulnerability that allowed the hack (outdated plugin, weak password) was still present after restoration. An attacker with a backdoor shell can re-infect a site within minutes of restoration if the underlying issue is not fixed.

How do I know if my site is fully clean after removal?

A clean site should: have no Google Safe Browsing warnings, show no unexpected pages in a site:domain.com search, have no redirect behaviour from any traffic source on any device, and return a clean result from Sucuri SiteCheck and VirusTotal. A professional cleanup service will confirm all of these before sign-off.

Is Your Website Redirecting Visitors? SEOHUB Will Fix It Fast.

Redirect hacks are one of the trickiest infections to fully remove. Our experts clean every trace
— including the backdoors that cause re-infection.
SHARE:

Leave a Comment

Your email address will not be published. Required fields are marked *