On this page
What this issue is
A conditional redirect hack is code added to your site by an attacker. It checks where the visitor came from, using the HTTP Referer header or document.referrer. If the visitor came from a search engine or an ad platform, the code sends them to a spam, scam or malware page. If you open the site directly, nothing happens.
- Ad or search clickVisitor arrives with a referrer
- Hidden redirectInjected code sees the referrer and fires
- Landing pageNever shown
- Form or checkoutNever reached
- ConversionNever recorded
The break happens only when a referrer is present, so direct checks miss it.
Why it costs you leads or sales
Every paid click that lands on a spam page is money spent for nothing. Organic visitors are lost the same way. Because you see nothing wrong, the problem can run for weeks while conversions fall and ad costs stay the same.
There is a second cost. Search engines and browsers can notice the behaviour and flag the site, which can bring warnings, ad disapprovals and ranking loss on top of the lost traffic.
Example: of 100 ad clicks, a referrer-only redirect sends most away while direct visits look healthy.
How to check if your site has it
You need to visit the site the way a customer does: arriving from a search result or an ad, in a clean browser with no logged-in admin session.
Use a clean browser profile
Open a private window, log out of your CMS and disable extensions. Admin sessions are often excluded from the hack.
Arrive from Google
Search for your brand or a page title, then click your result. Try on both mobile and desktop, because some hacks target mobile only.
Arrive from your ad
Open your own ad preview or tracked URL. Do not click your live ads repeatedly, as that wastes budget and may break ad platform rules.
Test with a fake referrer in the terminal
Run the curl command below and compare the response with a request that has no referrer.
Check Search Console
Open Security and manual actions, and use URL Inspection with Test live URL to see what Googlebot receives.
# Normal request
curl -sI https://example.com/ | head -n 10
# Pretend to come from Google
curl -sI -e "https://www.google.com/" -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com/ | head -n 10
# Look for a Location header or 301/302 status in the second result onlyHow to fix it
Removing the visible redirect is not enough. The attacker usually left a way back in. Work through cleanup and access in this order.
Take a backup and put the site in maintenance mode
Keep a copy of the infected state for investigation, and pause ad campaigns pointing at the site while you work.
Search for referrer checks
Look for
HTTP_REFERER,document.referrer,google,bing,gclid,base64_decode,eval(and unfamiliaratob(calls in files and in the database.Remove the malicious code
Delete injected lines, not just the whole file, unless it is a file that should not exist. Replace core, theme and plugin files with fresh copies from the official source.
Remove rogue users, plugins and scheduled tasks
Delete unknown admin users, unused plugins and unfamiliar cron jobs, which are common ways for the hack to return.
Change every credential
Reset passwords for the CMS, hosting, FTP/SFTP, database, email and tag manager. Rotate API keys and invalidate all sessions.
Update everything
Update the CMS, themes and plugins. Remove anything abandoned or pirated.
Request a review if flagged
If Search Console or Google Ads reported an issue, submit a review request after cleanup.
Check .htaccess
Open
.htaccessin the site root and look forRewriteCond %{HTTP_REFERER}lines. Restore the default WordPress rules.Check theme and plugin files
Inspect
header.php,footer.php,functions.phpand thewp-content/mu-pluginsfolder. Reinstall themes and plugins from fresh copies.Check the database
Search the
wp_optionsandwp_poststables for<scripttags you did not add, including the header and footer fields of any script-insertion plugin.
Review Custom HTML tags
Open your container and check every Custom HTML tag and Custom JavaScript variable for redirects such as
window.location.Check users and versions
Under Admin, review User Management and version history for changes you did not make. Remove unknown users.
Inspect theme code
In Online Store, Themes, Edit code, search
theme.liquidand assets fordocument.referrerandwindow.location.Review apps and staff
Remove unfamiliar apps, check Settings, Users and permissions, and look at Customer events for custom pixels you did not add.
Check custom code
Review Site settings, Custom code (head and footer) and each page's custom code for unknown scripts.
Check integrations and members
Remove unknown apps and collaborators, then reset your password.
Compare with version control
Run a diff against your last known good commit and redeploy from clean source.
Check server config
Review
.htaccess, nginx config and any includes for referrer conditions, and look for recently modified files.
grep -RniE "HTTP_REFERER|document\.referrer" --include=*.php --include=*.js --include=.htaccess /var/www/html | head -n 50
# Files changed in the last 14 days
find /var/www/html -type f -mtime -14 -not -path "*/cache/*" | head -n 100How to confirm it's fixed
- Click through from a Google result on mobile and desktop and reach the correct page
- Open a tracked ad URL and reach the correct landing page
- The curl test with a Google referrer returns the same response as without one
- A search for your code patterns returns only legitimate matches
- Search Console shows no security issues, and URL Inspection live test shows the real page
- A test form submission or checkout step still works end to end
- No unknown admin users, plugins or tag manager users remain
How to prevent it happening again
- Keep the CMS, themes and plugins updated and delete unused ones
- Use unique strong passwords and two-factor authentication on hosting, CMS and tag manager
- Limit admin accounts and review them monthly
- Turn off file editing in the CMS dashboard where possible
- Keep off-site backups so you can restore a clean version
- Review third-party scripts and tag manager changes regularly
- Test your landing pages from a search and ad arrival after every major change
How FunnelGuard handles this
FunnelGuard detects this only partly. It runs your real journeys with a robot visitor on a schedule, and a synthetic ad arrival can catch a redirect that fires for ad traffic: if the robot lands somewhere other than your page, the step fails and you get one alert with a screenshot and a likely cause, after a re-check from a second region.
The injected code reacts to the referrer and diverts the visitor before the page loads.
Questions people ask
Why does my website redirect only when I come from Google?
Most likely malicious code checks the referrer and redirects only visitors who arrive from search engines or ads. It hides the redirect from you and from direct visits. Search .htaccess, theme files, the database and your tag manager for referrer checks and unknown redirects.
Why is my site fine when I type the URL but redirects from ads?
A hack can be set to fire only when a referrer or ad parameter such as gclid is present. Typing the address sends no referrer, so the code stays dormant. Test with a clean private window, arriving from a search result or an ad preview.
How do I test my site for a referrer-based redirect?
Open a private window, log out of your CMS, and click through from a Google result on mobile and desktop. You can also run curl -sI -e https://www.google.com/ https://yoursite.com/ and look for a Location header or 301/302 status that does not appear without the referrer.
How do I remove a conditional redirect hack from WordPress?
Restore the default .htaccess, reinstall the theme, plugins and core from clean copies, and search the database for unknown script tags. Then delete rogue admin users and change all passwords. Without the credential reset, the hack often returns.
Can Google penalise my site for a redirect that only affects some visitors?
Yes. Sneaky redirects that send users somewhere different from what search engines see can bring security warnings, manual actions or ranking loss. Check Search Console's Security and manual actions reports and request a review after cleanup.
Can FunnelGuard detect a redirect that only happens for ad traffic?
Partly. Its synthetic ad arrival can catch a redirect that fires on ad-style visits, and it alerts you with a screenshot. It does not scan your files for malware and may miss hacks aimed at specific countries, devices or real search clicks, so keep checking by hand too.
Why did my ad conversions drop but my site looks fine?
If the site only misbehaves for visitors arriving from ads, your own visits will look normal while paid visitors are redirected. Compare ad clicks with landing page sessions and test a real ad arrival from a clean browser.
Related issues
- OPS-140Malware/redirect hack sending mobile/ad visitors elsewhereFix guide
- OPS-154SEO spam in title/meta for Google only (cloaking)Fix guide
- OPS-138Google Safe Browsing 'Deceptive site' warningFix guide
- OPS-139Chrome 'dangerous site' red screen on funnelFix guide
- OPS-150Search Console security issues / manual actionsFix guide
- OPS-147Injected popups / malvertisingFix guide