In this guide
- What .htaccess malware looks like
- Step 1: Back up and stop the bleeding
- Step 2: Find out which .htaccess files are malicious
- Step 3: Restore normal permissions
- Step 4: Stop whatever recreates the files
- Step 5: Delete the malicious copies and reset the main file
- Step 6: Clean the rest of the infection
- When to get help
What .htaccess malware looks like
The .htaccess file tells an Apache web server how to handle requests in a folder. Malware abuses it because it controls which files can run and who can see them. The typical picture:
- An
.htaccessfile in nearly every folder, often hundreds of identical copies, deep insidewp-content,wp-includesand plugin folders. - Rules that block every PHP file except a few: the attacker’s own files. Your site breaks or shows errors, while the malware keeps working.
- Locked permissions. Files and folders set to read-only, so you can’t edit or delete them.
- Files that come back shortly after you delete them.
It’s frustrating by design. The goal is to make cleanup painful enough that the owner gives up.
Step 1: Back up and stop the bleeding
Make a full backup of the current state, files and database, even though it’s infected. Then change your hosting, SFTP and WordPress passwords. If the site is public and showing spam or redirects, ask your host to put it in maintenance mode.
Step 2: Find out which .htaccess files are malicious
List every .htaccess file with its date and permissions:
find . -name ".htaccess" -printf '%TY-%Tm-%Td %TH:%TM %m %p\n' | sort | less
Malicious copies are usually identical. Group the files by content to see which version appears everywhere:
find . -name ".htaccess" -exec md5sum {} + | sort | uniq -c -w32 | sort -rn | head
The first column is how many copies share the same content. A version repeated in 50 or 500 folders is the malware. Open one and read it. You’ll usually see rules that deny access to all .php files except one or two specific names. Those names point you straight to the attacker’s files.
Keep: the .htaccess in your main WordPress folder, which you’ll reset in step 5, and files legitimate plugins create in their own folders, such as cache, backup or security plugins.
Step 3: Restore normal permissions
Set folders to 755 and files to 644 so you can work with them again:
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +
Your host may use slightly different defaults. If permission changes fail, the files may belong to a different system user, and you’ll need your host’s support to reset them.
Step 4: Stop whatever recreates the files
If you delete files before stopping their source, they’ll be back within minutes. Check:
- Running processes. Malware sometimes keeps a PHP process running in the server’s memory that rewrites the files. On a VPS, look with
ps aux. On shared hosting, ask support to kill all processes running under your account. - Cron jobs. In your hosting panel, look under Cron Jobs for anything you didn’t create. Check WordPress cron too:
wp cron event list --skip-plugins --skip-themes. - The attacker’s own files, the ones the
.htaccessrules allowed to run. Delete them now.
Step 5: Delete the malicious copies and reset the main file
Delete all copies of the malicious version at once, using the hash you found in step 2:
find . -name ".htaccess" -exec md5sum {} + | grep '^PASTE_THE_HASH_HERE' | cut -c35- | xargs -d '\n' rm -f
Then replace the main .htaccess in your WordPress folder with the standard WordPress rules (plus any rules your caching or security plugin needs):
# BEGIN WordPress
<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
Wait 15–30 minutes and check again. If new .htaccess files appear, something is still recreating them. Go back to step 4.
Step 6: Clean the rest of the infection
The .htaccess files are only the lock on the door. Behind them is a normal WordPress infection: backdoors, modified core files, maybe spam or redirects in the database. Work through my step-by-step WordPress malware removal guide to clean the rest, and find out how the attacker got in. In my experience, it’s very often an outdated plugin.
A common story: an owner on shared hosting spends two days deleting .htaccess and PHP files from deep inside every folder, fixing permissions five levels down. The next morning, everything is back. The host quotes several hundred dollars to clean it. The missing piece is almost always a process or cron job still running, or one backdoor outside the folders they checked. Once that’s stopped, the bulk deletion takes minutes instead of days.
When to get help
If files keep coming back after step 4, you’re on shared hosting without SSH, or the site is an online store, it’s usually faster to hand it over. I clean this kind of infection regularly. It’s $95 per site with a 1-month guarantee, which is usually well below what hosts quote. Send me the details.