Visitors land on a lottery page, a pill shop or a fake tech support screen, yet the site looks fine when you open it. That is the mark of an injected redirect, usually a conditional one. This guide shows how to trigger it on purpose, locate it in the files or the database, and shut the door it came through.
wp-config.php..htaccess, the PHP files, the database and the site address settings.wp-content/uploads, plugins to reinstall or delete.A redirect planted during a hack picks its targets. It tends to spare the person who runs the site, because that keeps it in place longer. The usual conditions:
Referer request header.User-Agent header.So when a customer writes "your site sends me somewhere else", take it at face value, even if everything looks right on your screen.
Put yourself in the shoes of the visitor who gets redirected:
If you have a terminal, this command requests your home page while posing as an iPhone coming from Google:
curl -s -i -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)" -e "https://www.google.com/" https://www.your-site.com/
Read the first lines of the response. A 301 or 302 status followed by a Location: line pointing somewhere unknown means the server does the redirecting: look in .htaccess or a PHP file. A 200 status while the browser still gets redirected means the redirect runs inside the page, in JavaScript: it usually lives in the database or in a theme file.
DB_PASSWORD in wp-config.php), and every WordPress administrator account.AUTH_KEY to NONCE_SALT in wp-config.php with a fresh set from https://api.wordpress.org/secret-key/1.1/salt/. Every open session is logged out, including the intruder’s.wp config shuffle-salts
At the site root, and sometimes in a subfolder, .htaccess can carry rewrite rules added by the attacker. The block WordPress writes itself is short and sits between # BEGIN WordPress and # END WordPress:
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
A RewriteCond testing %{HTTP_REFERER} (google, bing, facebook) or %{HTTP_USER_AGENT} (android, iphone, mobile), followed by a RewriteRule to an outside address, is the redirect. Caching and security plugins also write blocks of their own, labelled with their name: match each block against your plugin list before removing it.
WP-CLI checks WordPress core and plugin files against the checksums published by WordPress.org and lists every file that was changed or added:
wp core verify-checksums
wp plugin verify-checksums --all
The second command covers plugins distributed through the WordPress.org directory. A plugin bought from its vendor is compared against a fresh copy downloaded from your account with that vendor. This is common: among the 540 SMB WordPress sites in our survey of 1 September 2026, 66% run at least one plugin that is not in the public directory.
Without WP-CLI, two searches give you a first shortlist:
find . -name "*.php" -mtime -30
find wp-content/uploads -name "*.php"
The first lists PHP files modified in the last 30 days, the second lists PHP files stored in the media folder, where they are suspicious by default (some plugins place an empty index.php there, which is fine). Modification dates can be faked, so an old file in the wrong place still deserves a look.
Open these first: wp-config.php, the root index.php, the active theme’s functions.php and header.php, and the wp-content/mu-plugins folder, whose plugins load automatically and cannot be switched off from the dashboard. Things to look for: eval(, base64_decode(, gzinflate(, str_rot13(, long unreadable strings, or window.location followed by an address you do not recognize.
A JavaScript redirect is often stored in post content or in the settings, where no file will show it. WP-CLI searches every WordPress table for a string:
wp db search "<script"
wp db search "fromCharCode"
Without WP-CLI, run the same searches from phpMyAdmin, in the SQL tab:
SELECT ID, post_title FROM wp_posts WHERE post_content LIKE '%<script%';
SELECT option_name FROM wp_options WHERE option_value LIKE '%<script%';
Some hits are legitimate: an analytics tag, a cookie banner, a widget you added. Anything that matches nothing you installed deserves a closer look, especially a script that builds an address out of character codes.
Under Settings > General, "WordPress Address (URL)" and "Site Address (URL)" must show your own domain. The same values can be forced in wp-config.php with the WP_HOME and WP_SITEURL constants, so check both places.
wp option get home
wp option get siteurl
wp user list --role=administrator
When the constants are defined, the first two commands return their values. The last one lists administrators with their registration date; in the dashboard, the same list comes from Users, filtered on the Administrator role. An account nobody recognizes gets deleted, with its content attributed to a legitimate account.
Deleting the line that redirects stops the symptom. The access the attacker used stays open until you find it, and the redirect comes back within days. The order that holds:
wp-admin and wp-includes folders, then run wp core download --skip-content --force, which rewrites core without touching your plugins, themes and media. The command overwrites existing files but deletes nothing that was added, which is why the two folders go first, and why unknown PHP files at the root are removed by hand.wp-content/uploads, the rules added to .htaccess and the scripts found in the database.wp cron event list: a task with an unfamiliar name can put the code back.If the site holds personal information (forms, customer accounts, orders), write down when you discovered the hack and what was exposed. Under PIPEDA, the federal private-sector privacy law, a breach of security safeguards involving personal information must be reported to the Privacy Commissioner, and the people affected notified, as soon as feasible once it is reasonable to believe the breach creates a real risk of significant harm to someone. Every breach, reported or not, must also be recorded, and the record kept for two years.
Once Google detects the redirect, Chrome can show a red "Dangerous site" page before opening the site, and Google can write "This site may be hacked" under your search result. The Security issues report in Google Search Console shows what was found and on which URLs. When the site is clean, the Request review button starts the check.
With the redirect gone, one question remains: who looks after the site from now on? Simafri takes over your WordPress site, puts it back online on a base we host, secure and keep up to date, and looks after it month after month. You keep your domain name and your content.
The injected code reads the browser User-Agent and only redirects phones, so the person managing the site, often on a computer, sees nothing. Test from a phone on mobile data, or with curl sending a mobile browser identifier.
Reinstalling core rewrites the WordPress files without deleting the ones an attacker may have added. The database, the theme, the plugins and the media folder stay as they are, and that is where the redirect and the attacker’s access usually sit. A fresh core is one step of the cleanup, alongside the database, the plugins, the accounts and the passwords.
Because the access used to plant it is still there: an added administrator account, a PHP file hidden among the media, a scheduled task, a vulnerable plugin. Any of them lets the code be put back after the cleanup. Check the accounts, the wp-content/uploads folder, the scheduled tasks and the plugin versions, then change the passwords again.
Open Google Search Console and go to the Security issues report: it lists the problems found and sample URLs. If the site is not verified in Search Console yet, verifying through a DNS record covers the whole domain.
Simafri
Let's talk about your website
Tell us about your project in a few words: we will get back to you quickly.
Thank you! Your request has been sent. We'll get back to you shortly.
Prefer email? Write to us at support@simafri.com.