Hacked .htaccess File in WordPress: How to Spot and Fix It

Hacked .htaccess File in WordPress: How to Spot and Fix It

Table of Contents

Your site looks fine when you type the address into your browser. But when someone clicks your link on Google, they land on a spam or scam website. Or your site suddenly shows a 500 Internal Server Error, even though you didn’t change anything. In many of these cases, the problem is a tiny hidden file called .htaccess.

A hacked .htaccess file is one of the most common and most confusing types of WordPress infection. The malicious rules are only a few lines long, they often target only some visitors, and the file is hidden by default in many file managers. In this guide, we explain what the file does, how to spot a hacked .htaccess file, and how to clean it step by step so it doesn’t come back.

What Is the .htaccess File in WordPress?

The .htaccess file is a configuration file used by Apache and LiteSpeed web servers. It tells the server how to handle requests before WordPress even loads. WordPress uses it mainly for pretty permalinks, so a URL like /my-post/ is sent to index.php instead of returning a 404 error.

A clean WordPress root .htaccess file usually contains a block that looks like this:

# BEGIN WordPress

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]

# END WordPress

This is the standard block shown in the official WordPress .htaccess documentation. Caching, security and SEO plugins often add their own clearly labeled sections, such as # BEGIN LSCACHE or # BEGIN Wordfence. Those are normal. If your site is on Nginx only, WordPress doesn’t use .htaccess at all, but you may still find malicious copies in your folders.

Why Hackers Target .htaccess

Because the server reads .htaccess before WordPress runs, any rule an attacker adds there works even if your WordPress files are clean. Attackers use it to:

  • Redirect visitors from Google, Bing or social media to spam, scam or malware sites
  • Redirect only mobile users, so the site owner on a desktop never notices
  • Send 404 pages to an external site using an ErrorDocument rule
  • Run PHP from image files, so a backdoor named like a .jpg or .ico can execute
  • Load malware before every page with a php_value auto_prepend_file rule
  • Lock you out of your own site by blocking PHP in every folder except their own malicious files

Search engine redirects are the most common result. If that’s what you’re seeing, our guide to the WordPress redirect hack covers the other places redirect code can hide.

What attackers do with a hacked .htaccess file in WordPress: redirects, 404 hijacks and PHP in images
The most common ways attackers abuse the .htaccess file.

Signs of a Hacked .htaccess File

Watch for these symptoms:

  • Visitors from Google are redirected, but typing your URL directly works fine
  • Your site redirects on phones but not on your computer
  • A 500 Internal Server Error appears on every page, or only in wp-admin
  • Broken links or missing pages send visitors to another domain
  • Google shows a “This site may be hacked” message in search results, or Chrome shows a red Deceptive site ahead warning
  • You find .htaccess files in folders that never had one, such as every plugin or uploads subfolder
  • The file comes back with the same bad rules a few minutes after you clean it

Attackers often hide the malicious rules far down the file, after hundreds of blank lines, or in the middle of a legitimate plugin block. Always scroll to the very end of the file and check its full length.

What Malicious .htaccess Rules Look Like

Here are the patterns that should make you suspicious:

  • RewriteCond %{HTTP_REFERER} lines that check for google, bing, yahoo, facebook or similar sites
  • RewriteCond %{HTTP_USER_AGENT} lines that target android, iphone or mobile
  • RewriteRule lines that send visitors to a domain you don’t own
  • ErrorDocument 404 or ErrorDocument 403 pointing to an outside URL
  • AddHandler or AddType lines that tell the server to run .jpg, .png, .ico or .txt files as PHP
  • php_value auto_prepend_file or auto_append_file pointing to a strange file
  • <FilesMatch> blocks that deny all PHP files, followed by a rule that allows only one or two oddly named files
Example of malicious redirect rules in a hacked .htaccess file
Suspicious conditions in yellow, malicious rules in red, all placed above the normal WordPress block.

One rule on its own doesn’t always prove a hack. Some plugins use referrer checks for legitimate reasons. The question to ask is simple: would you or one of your plugins ever send visitors to that domain? If not, it’s malicious.

How to Fix a Hacked .htaccess File: Step by Step

Before you edit anything, download a full backup of your files and database and label it as infected. If your site is actively redirecting visitors, our checklist on what to do when your WordPress site is hacked covers the first 24 hours.

Step 1: Show hidden files

Files that start with a dot are hidden by default. In cPanel File Manager, open Settings and tick Show Hidden Files. In FileZilla, enable Server > Force showing hidden files. Other control panels have a similar option.

Step 2: Find every .htaccess file

The malicious file in your root is rarely alone. If you have SSH access, list every copy on your site with find . -name ".htaccess" -type f, and list the ones changed in the last 30 days with find . -name ".htaccess" -mtime -30. Then search them all for the most common malicious patterns:

grep -rlE --include=.htaccess "HTTP_REFERER|HTTP_USER_AGENT|auto_prepend_file|ErrorDocument|AddHandler" .

Review each result by hand. Legitimate plugins may match one of these patterns too.

Step 3: Replace the root .htaccess file

Open the .htaccess file in your WordPress root. Copy any rules you know you need, such as your HTTPS redirect or rules added by your cache plugin, into a text file on your computer. Then delete everything in the file and paste in the clean default block shown above, followed only by the rules you trust.

You can also let WordPress rebuild it. Rename the old file to .htaccess-infected, then go to Settings > Permalinks in your dashboard and click Save Changes. With WP-CLI, wp rewrite flush --hard can regenerate it too, although on some servers WP-CLI needs extra configuration before it will write the file.

Step 4: Clean or delete the extra .htaccess files

Check every .htaccess file outside the root. Files in folders like wp-content/uploads are sometimes created by security or backup plugins to block access, so compare them with the plugin’s documentation before deleting. Remove any file that redirects visitors, runs images as PHP or blocks every PHP file except a few unknown ones. Delete the unknown files those rules point to as well.

Step 5: Check .user.ini and php.ini

On many hosts, attackers use .user.ini or php.ini instead of .htaccess to set auto_prepend_file. Search for them with find . -name ".user.ini" -o -name "php.ini" and remove any line that loads a file you don’t recognize.

Step 6: Find what keeps rewriting the file

If your hacked .htaccess file comes back after you clean it, something on the server is rewriting it. That’s almost always a backdoor, a malicious plugin or an infected must-use plugin. Our guide on finding a WordPress backdoor shows where to look, and our WordPress malware removal guide walks through the complete cleanup of files and the database.

Step 7: Test as a real visitor

Clear your site cache and any CDN cache. Then test from a private browser window, from your phone on mobile data and by clicking your site in Google search results. You can also check the redirect from the command line with curl -I -A "Mozilla/5.0 (iPhone)" -e "https://www.google.com/" https://example.com/, replacing example.com with your domain. A clean site returns a 200 status, or at most a redirect within your own domain, not a 301 or 302 to another domain.

How to Protect .htaccess After a Hack

  • Use sensible permissions. 644 is the usual value. Some owners set it to 444 after cleaning, which can slow down simple malware, but it won’t stop code running under your account from changing it back.
  • Turn on file change monitoring so you’re alerted when .htaccess changes.
  • Update WordPress, plugins and themes every week and delete what you don’t use.
  • Change all passwords for WordPress, hosting, SFTP and the database after the cleanup.
  • Never install nulled plugins or themes. They are a common way backdoors get onto a site.

If the hack keeps returning no matter what you do, read why a WordPress site keeps getting hacked.

Frequently Asked Questions

Can I just delete the .htaccess file?

You can delete the root file and let WordPress recreate it from Settings > Permalinks. That removes the bad rules, but you’ll also lose any legitimate rules from your host or plugins, so save those first. Deleting it won’t remove the backdoor that created the hack.

Why does the redirect only happen to some visitors?

Malicious rules often check where the visitor came from or what device they use. Attackers do this so site owners, who usually type the URL directly on a desktop, don’t notice the problem.

Why does my hacked .htaccess keep coming back?

Because malware somewhere else on the server is rewriting it. Cleaning the file without removing that malware only fixes the symptom. Look for backdoors, unknown plugins, cron jobs and infected sites on the same hosting account.

My host uses Nginx. Can .htaccess still be hacked?

Nginx ignores .htaccess files, so malicious rules in them won’t run. However, attackers may still use PHP code, the database or .user.ini to cause the same redirects, so the rest of the site still needs checking.

Will Google remove the hacked warning after I fix .htaccess?

Not automatically. Once the whole site is clean, request a review in Google Search Console. Our guide to Google Search Console security issues explains how.

Need Your Hacked .htaccess Fixed Today?

A hacked .htaccess file is usually the visible part of a bigger infection. We clean every .htaccess and config file, remove the backdoors that keep rewriting them, check your database and secure the site so the redirects stop for good. Get started with our hacked website repair service, or choose emergency malware removal if visitors are being redirected right now.

Share This Post

Facebook
LinkedIn
Need Help? We're Here!

If your website is hacked or showing malware warnings, don’t panic. Contact us now and we will clean it for you.