WordPress hacked redirect: how to find it and remove it
Your WordPress site sends visitors to a scam, a fake shop or spam, often only on phones. Where the redirect hides and how to remove it so it does not come back.
If your WordPress site sends visitors to another site and nobody set up that redirect, it is almost always malicious code. It usually sits in one of five places: the .htaccess file, the theme files, an unknown or modified plugin, the database (post content, settings, the site address) or a script added to wp-config.php or index.php. Removing the redirect is not the end of it: you also have to find the backdoor that put it there.
First make sure it is the site, not your phone
A redirect that only you see, on one device, on every site you visit, is more likely an ad in your browser or an extension. A redirect your customers see on different devices, and only on your site, is a problem with the site.
Malicious redirects are written to be hard to reproduce. Often they fire only:
- on phones,
- for people who arrive from Google results rather than typing the address,
- once per visitor, because the script sets a cookie and does not redirect the second time,
- for visitors who are not logged in, so an administrator sees nothing.
To test it, use a phone on mobile data, open a private tab, search for your business on Google and tap the result. Write down the address you landed on and the time. If a customer reports a redirect you cannot reproduce, do not assume they were wrong.
Where to look
This part needs access to the files (FTP or the hosting file manager). Before you change anything, copy the files and the database as they are.
The .htaccess file. Open .htaccess in the site's root folder. The standard WordPress block starts with # BEGIN WordPress. Lines with RewriteCond %{HTTP_REFERER} and a foreign domain in a RewriteRule, especially conditions on Google or phones (HTTP_USER_AGENT with android or iphone), are a typical redirect. Check .htaccess files in subfolders too.
The site address. In Settings > General both "WordPress Address" and "Site Address" must be your domain. If someone changed them, the whole site, dashboard included, moves elsewhere.
The theme. The files changed most often are header.php, footer.php and functions.php of the active theme. Look for scripts with foreign domains, long unreadable strings and calls to eval, base64_decode or String.fromCharCode. The safest fix is to reinstall the theme from the vendor's package.
Plugins. Go through the plugin list and the wp-content/plugins folder. An unknown plugin can have an innocent name. Check wp-content/mu-plugins as well, because plugins there always run and are not in the normal list. A developer verifies directory plugins with wp plugin verify-checksums --all and core with wp core verify-checksums.
The database. A redirect script is often added to post content, widgets or theme options. A developer searches the wp_posts and wp_options tables for <script and for the domain visitors end up on.
Root files. index.php, wp-config.php and wp-blog-header.php in the root folder should have nothing added at the top or bottom. Compare them with the same WordPress version's package.
After the redirect is gone
The redirect is the effect. So it does not come back next week:
- Delete administrator accounts nobody recognises and check
wp-content/uploadsfor PHP files. That is where the backdoor usually is. - Change the passwords for the dashboard, hosting, FTP and database, and the security keys in
wp-config.php. - Update WordPress, plugins and the theme, and delete plugins you do not use.
- Clear every cache: the caching plugin, the host's cache and the CDN if you use one. Otherwise visitors keep getting the old, infected version for a while.
The full cleanup order is in WordPress site hacked: how to fix it.
If Google has already flagged the site, request a review in the Security issues report in Search Console. The details are in Google says your site is dangerous.
When it keeps coming back
If the redirect returns a few hours later and files are changed again, a backdoor or a cron job is still on the server and downloads the code again. At that point you need the server logs and a look at the whole hosting account, including other sites stored on it. Old copies and test sites nobody uses are a common source.
Common questions
Why does the redirect only happen on phones?
Because the malicious code was written that way. It checks the device and where the visitor came from, so the site owner notices as late as possible.
Can a redirect plugin be doing this?
Yes, if someone added a rule to it, so check its list of redirects. More often, though, the redirect is in the code, not in a plugin's settings.
Were my customers infected?
The sites they land on are usually scams and fake shops, sometimes attempts to get them to install a malicious app. If you know customers complained, send them a short note saying the problem was on your side and has been removed.
If you would rather not do it yourself
The free website check looks at the site from the outside and shows, among other things, whether the home page code contains suspicious redirects in scripts and whether the domain is on threat lists. No password and no account. Its limit: redirects that only fire for phones arriving from Google may not show up in it.
If you want someone to remove the redirect and what caused it, order a hacked site fix. From $99, a quote within 1 business day, and you pay only after you accept. To keep an eye on files and accounts afterwards, the free Ensomedia Security plugin runs inside WordPress.