How to fix WordPress redirecting to spam sites
When a WordPress site redirects visitors to spam, gambling, fake prize or adult sites, it is almost always a malicious redirect injected by an attacker. This guide explains how to fix WordPress redirecting to spam sites: where the redirect usually hides, how to find it, and how to remove it for good.
1. Understand how the redirect behaves
Many redirect infections are "conditional", so the site may look normal to you. Test like a visitor:
- Open the site in a private/incognito window while logged out of WordPress.
- Try on a mobile phone using mobile data, not your office Wi-Fi.
- Search for your site on Google and click the result. Some infections only redirect visitors coming from search engines.
- Note whether the redirect happens instantly (server side) or after the page starts loading (JavaScript).
2. Check the .htaccess file
- Log in to cPanel (see how to log in to cPanel) and open File Manager.
- Enable Show Hidden Files in the settings, then open
public_html/.htaccess. - A standard WordPress
.htaccesscontains the# BEGIN WordPressblock plus rules added by caching or security plugins. Look forRewriteCondlines that checkHTTP_REFERERorHTTP_USER_AGENTfor google, android or iphone and then redirect to an unknown domain. - Remove the malicious lines. Also check for extra
.htaccessfiles in subfolders such aswp-contentandwp-content/uploads.
3. Check the site URL settings in the database
Some attacks change your site address so every page points elsewhere. In phpMyAdmin, run (adjust the wp_ prefix if yours is different):
SELECT option_name, option_value FROM wp_options
WHERE option_name IN ('siteurl','home');
Both values must be your own domain, for example https://yourdomain.com.
4. Search for injected JavaScript
Many redirects are small scripts added to posts, widgets or theme files. 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%';
Some results may be legitimate (for example a tracking code you added on purpose). Look for obfuscated code, eval(, atob(, String.fromCharCode or unknown external domains. Also check the theme's header.php, footer.php and functions.php, and any "header/footer scripts" plugin.
5. Check plugins, themes and users
- Look for plugins you did not install, including the hidden
wp-content/mu-pluginsfolder. - Check Users for administrator accounts you do not recognise.
- Re-install WordPress core from Dashboard > Updates, and re-install plugins and themes from official sources.
- Search uploads for PHP files, which should not exist there:
find ~/public_html/wp-content/uploads -type f -name "*.php"
6. Purge caches and secure the site
- Purge LiteSpeed Cache (or any caching plugin) and Cloudflare cache if you use it, otherwise visitors may still get the old infected page.
- Change all passwords: cPanel, WordPress admins, FTP and database.
- Update everything and follow how to secure your WordPress website.
- If Google shows a warning, request a review in Search Console after cleaning.
Common problems
- The redirect comes back after a few hours: a backdoor is re-infecting the site. Check cron jobs in cPanel, mu-plugins and uploads, or follow how to clean a hacked WordPress site.
- Only some visitors are redirected: the malware targets mobile or search traffic, or caches the redirect per visitor. Test as described in step 1.
- You only see the redirect in one browser: clear that browser's cache and check its extensions; the problem may be on the device, not the site.
Frequently asked questions
Why don't I see the redirect when I am logged in?
Many infections deliberately skip logged-in administrators so the owner does not notice. Always test logged out in a private window.
Will my Google ranking recover?
Usually yes, once the site is clean and Google re-crawls it. Fix the issue quickly and request a review if Search Console shows a security warning.
Can EgWeb clean it for me?
Yes. Open a support ticket with your domain and a description of when the redirect happens, and our team will advise you on the cleanup.
Need help? Open a support ticket or message us on WhatsApp +20 127 113 3655.