WordPress Security ServicesMalware Removal
Malware Removal Guide

WordPress .htaccess Malware in Every Folder: How to Remove It for Good

You open your file manager and every single folder has an .htaccess file it shouldn't have. Some can't be deleted. The ones you do delete come back. Your site is broken or locked, and your host wants hundreds of dollars to fix it. This is a well-known WordPress infection, and it can be cleaned if you work through it in the right order.

Sergey By Sergey, WordPress security specialist Updated 4 min read
In this guide
  1. What .htaccess malware looks like
  2. Step 1: Back up and stop the bleeding
  3. Step 2: Find out which .htaccess files are malicious
  4. Step 3: Restore normal permissions
  5. Step 4: Stop whatever recreates the files
  6. Step 5: Delete the malicious copies and reset the main file
  7. Step 6: Clean the rest of the infection
  8. 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 .htaccess file in nearly every folder, often hundreds of identical copies, deep inside wp-content, wp-includes and 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 .htaccess rules 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.

Sergey, WordPress Security Specialist
About the author
Sergey, WordPress Security Specialist

10 years in WordPress security and incident investigation, and more than 200 websites cleaned of malware. Top Rated on Upwork with a 100% Job Success score. More about me

Frequently Asked Questions

Why are there .htaccess files in every folder of my WordPress site?

Because malware put them there. A normal WordPress site has one .htaccess in the main folder, plus a few created by specific plugins. Hundreds of identical files spread across every folder are a sign of an infection that uses them to protect its own files and block yours.

Why can't I delete the .htaccess files?

The malware usually changes the folder and file permissions so they're read-only. You need to restore normal permissions first (755 for folders, 644 for files), and then the files can be deleted. On some hosts, you may need support to do this for you.

Why do the files come back after I delete them?

Something is still running and recreating them: a PHP process that's still active on the server, a cron job, or a backdoor file you haven't found yet. Deleting the .htaccess files treats the symptom. You have to stop the process and remove its source.

Can I just restore a backup?

You can, but first delete the infected files completely, as hosts like HostGator also advise. Restoring over the top of an infected site leaves the malware's extra files in place. And restore from a backup made before the infection, then fix the entry point, or it will happen again.

Which .htaccess files should I keep?

The one in your main WordPress folder (cleaned back to the standard WordPress rules), plus ones that legitimate plugins create, for example in cache, backup, or security plugin folders. If dozens of folders contain identical copies of the same file, those are almost certainly malicious.

Need a hand?

Let me clean your WordPress site for you

Send me your site URL and what you're seeing. I'll reply with what I think is going on and the next step.

$95 per infected site · 1-month guarantee · direct contact with the specialist

Get My Site Cleaned →