Your WordPress .htaccess file is a few lines of plain text in the site's root folder. It's easy to forget it exists. Then a restore or a host move goes wrong, and every page except the homepage returns a 404.
That's the failure this guide is about. Most articles on WordPress .htaccess are snippet libraries: here's how to block an IP, here's how to force HTTPS. Useful, but they skip the moment the file actually hurts you. That moment is when the database and the uploads come back from a backup and the hidden dotfile doesn't.
Below you'll find what the file does, the default WordPress block copied from the official handbook, the custom rules that tend to pile up in it, why dotfiles go missing in backups, how to check yours, and the fast fix when it's gone. If your host runs Nginx, there's a section for you near the end too.
What .htaccess does on a WordPress site
According to the Apache documentation, .htaccess files "provide a way to make configuration changes on a per-directory basis" without editing the main server config. You drop one into a folder, and its directives apply to that folder and every folder beneath it. The Apache HTTP Server 2.4 tutorial on .htaccess files is the primary source here, and it's worth ten minutes of your time.
WordPress leans on that per-directory power for one big job: pretty permalinks. A URL like /blog/my-post/ doesn't match any real folder on disk. There's no blog directory and no my-post directory. Apache would normally answer that request with a 404. The rewrite rules in .htaccess tell Apache to hand any request that isn't a real file or folder to index.php instead, and WordPress then works out which post you asked for.
So the file is the bridge between the URLs your visitors and Google know and the single PHP entry point that serves them. Remove the bridge and the homepage still loads, because / maps to index.php directly. Everything else breaks.
On most real sites the file also does a lot more than permalinks. Redirects from an old URL structure, HTTPS enforcement, security rules, caching headers and PHP settings all end up in there over the years. Some of that was added by you, some by a developer you hired years ago, and some by plugins you've since forgotten about.
Where the file lives
It sits in the same folder as wp-config.php, wp-load.php and the wp-admin directory. On a cPanel host that's usually public_html/. If WordPress lives in a subfolder, the file is in that subfolder.
You won't see it by default. Files whose names start with a dot are hidden on Linux and macOS, and most hosting file managers and FTP clients hide them unless you tick a "show hidden files" option. That hiding is the root of the whole problem, as you'll see in a minute.
The default WordPress block (and what plugins add)
Per the WordPress Advanced Administration Handbook, this is the basic block for a single-site install. The handbook page is titled Apache HTTPD / .htaccess, and it says outright that it "may be used to restore a corrupted .htaccess file."
# 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
Read it line by line and it's less cryptic than it looks:
RewriteEngine Onswitches on Apache'smod_rewritemodule for this folder.- The
HTTP_AUTHORIZATIONline passes the Authorization header through to PHP. Application passwords and some REST API clients need it. RewriteBase /says the rules are relative to the site root. A WordPress install in/blog/would haveRewriteBase /blog/here, and a final rule of/blog/index.php.- The
^index\.php$rule stops Apache from rewritingindex.phpto itself in a loop. - The two
RewriteCondlines check that the request is not an existing file (!-f) and not an existing directory (!-d). Real images, CSS and thewp-adminfolder get served as normal. - The last rule sends everything else to
index.php.
The # BEGIN WordPress and # END WordPress markers matter. WordPress rewrites only the text between them when you save permalinks. Anything you put outside those markers is left alone. Anything you put inside them can be overwritten without warning.
Multisite networks use a longer block. The handbook gives separate versions for subfolder and subdomain networks on WordPress 3.5 and later, with extra rules for wp-admin trailing slashes and for routing wp-content, wp-admin and wp-includes requests per site. If you run a network, copy the exact version from the handbook. Don't adapt the single-site block by hand.
What usually piles up around it
Open the .htaccess of a site that's been live for a few years and the WordPress block is often the smallest part. Here's what tends to sit around it:
- Caching plugins. Page-cache plugins often write their own
# BEGIN/# ENDsections with rules that serve pre-built HTML files straight from disk, plus browser-cache headers and compression settings. - Security plugins. Rules that block access to
xmlrpc.php, deny direct requests forwp-config.php, or stop PHP from running insidewp-content/uploads/. - SEO redirects.
Redirect 301lines or rewrite rules from an old URL structure, a renamed category, or a domain change. Some redirect plugins store rules in the database, but plenty write them here. - HTTPS and www rules. A rewrite that forces every request to
https://and to one hostname. - PHP settings. On some hosts you'll find
php_value upload_max_filesize 64M,php_value memory_limit 256Mor a host-specificAddHandlerline that picks the PHP version. Delete that last one by accident and the site can silently drop to an older PHP release. - Password protection.
AuthType Basiclines that lock a staging copy or thewp-adminfolder.
None of these live in the database. If the file goes missing, they all go with it. The 404s are just the most visible symptom.
Why dotfiles go missing in backups and migrations
This is the part the snippet guides skip. The file doesn't usually get deleted. It gets left behind. There are four common ways that happens.
1. The backup only took the folders it knew about
Some backup tools think of WordPress as "the database plus wp-content." That covers posts, themes, plugins and uploads, which is most of what makes a site unique. It doesn't cover root-level files: .htaccess, wp-config.php, a robots.txt you edited, a Google verification file, or a .user.ini with PHP settings. Restore that backup onto a fresh WordPress install and the root files are whatever the fresh install shipped with. A fresh install has no .htaccess at all until you save permalinks.
We wrote about the database half of this trap in a database backup is only half a website. Root-level dotfiles are the quieter cousin: small, easy to miss, and painful when they're gone.
2. A glob pattern that skips dotfiles
Shell wildcards don't match hidden files by default. If someone archives a site with a command like zip -r site.zip public_html/* or copies with cp -r public_html/* /backup/, the * expands to every visible name and skips anything starting with a dot. The archive looks complete, the file count looks about right, and .htaccess isn't in it.
Archiving the folder itself (tar -czf site.tar.gz public_html) doesn't have this problem, because tar walks the directory and includes hidden entries. The difference between those two commands is one character, and it decides whether your rules come back.
3. The file manager or FTP client hid it
Manual migrations are where this bites hardest. Someone opens FileZilla or the cPanel File Manager, selects everything they can see in public_html, downloads it, and uploads it to the new host. With hidden files switched off, "everything they can see" excludes .htaccess. The new site loads its homepage, the person doing the move clicks around the admin, it all looks fine, and they call it done.
4. The new host overwrote it
Some hosts drop their own default .htaccess into a new account or a one-click WordPress install. If you copy your old files over the top, yours wins. If you restore only the database and wp-content onto that host's install, theirs stays, and all your custom rules are gone even though a file exists. That one is sneaky because a quick "is the file there?" check passes.
Host moves have plenty of other ways to go wrong, and our guide to moving WordPress to a new host without a maintenance window walks through the whole sequence. For this post, the point is narrow: the file that controls your URLs is the one most likely to be invisible during the move.
How to check your backup contains .htaccess
Don't trust the backup tool's summary screen for this. Open the archive and look. It takes a couple of minutes, and you only need to do it once per backup method.
Test setup
Here's a simple check you can run on any backup that produces a .tar.gz or .zip file. You need the archive downloaded to your computer or available over SSH.
For a .tar.gz archive:
tar -tzf site-backup.tar.gz | grep -E '(^|/)\.htaccess$'
For a .zip archive:
unzip -l site-backup.zip | grep '\.htaccess'
You should see at least one line ending in .htaccess at the WordPress root, for example public_html/.htaccess. Many sites also have one inside wp-content/uploads/ (often added by a security plugin to block PHP there), so seeing several results is normal.
No output means the root file isn't in the archive. That's your answer, and it's much better to learn it on a quiet afternoon than during a restore.
Then go one step further and compare contents, not just presence. Extract the file from the archive and diff it against the live one:
tar -xzf site-backup.tar.gz public_html/.htaccess -O > backup.htaccess
diff backup.htaccess /path/to/public_html/.htaccess
No output from diff means they match. If they differ, check whether the differences are recent changes (expected) or a missing chunk of rules (a problem with how the backup was taken).
Check the other root files while you're there
While the archive listing is open, look for these too. They live next to .htaccess and go missing for the same reasons:
wp-config.php, which holds the database credentials and salts.user.iniorphp.ini, if your host uses one for PHP limitsrobots.txt, if you have a physical file rather than the one WordPress generates- any search-console or domain-verification HTML files
How BackupYourSite handles it
When we built BackupYourSite's SSH service, our team made a deliberate choice: it archives your full application folder rather than a hand-picked list of WordPress directories. For WordPress it reads the database credentials from wp-config.php at run time and includes wp-config.php itself, because a restore needs it. Regenerable cache folders are the only thing excluded automatically. Every archive is integrity-checked before the run counts as a success. You can read the full list on our backup features page.
The check above is worth running on any backup, including ours. Opening the archive yourself is the only proof that counts.
Symptoms of a missing or broken .htaccess after a restore
The symptoms are distinctive once you know them. Here's how to read what you're seeing.
Homepage works, every other page 404s
This is the classic sign of a missing WordPress block. The homepage maps straight to index.php; posts and pages rely on the rewrite. If /?p=123 loads a post but /my-post/ doesn't, you've confirmed it. The content is there and only the URL routing is broken.
Old URLs that used to redirect now 404
Your main pages load, but links from old blog posts, backlinks from other sites, or bookmarks to a renamed page all hit a 404. That points to lost custom redirect rules, even if the WordPress block itself came back. Search Console's page indexing report will start listing these as "Not found (404)" over the following days and weeks as Google recrawls them.
Site loads on http:// and https:// both
If the HTTPS force rule lived in .htaccess, a restore without it leaves both versions reachable. Not dangerous on its own, but it splits signals between two URL versions and can trigger mixed-content warnings.
500 Internal Server Error on every page
This is the opposite problem: the file is there but contains something the server won't accept. Common causes are a php_value line on a host that runs PHP as FastCGI (where those directives aren't allowed in .htaccess), a rule that references an Apache module the new server doesn't load, or a copy-paste that broke a line. The Apache error log will name the offending line. The fast diagnosis is to rename .htaccess to .htaccess-old. If the site loads, the problem is in the file.
The rules seem to be ignored entirely
Per the Apache docs, the default value of AllowOverride is None, which means .htaccess files "are completely ignored unless you explicitly enable them." On shared cPanel hosting this is almost always enabled for you. On a fresh VPS you set up yourself, it often isn't. If your file is present and correct but nothing in it takes effect, check the virtual host config for AllowOverride All on your site's directory, and confirm mod_rewrite is loaded.
The two-minute fix when the file is gone
If you've confirmed the file is missing, the WordPress block comes back in seconds:
- Log in to wp-admin.
- Go to Settings → Permalinks.
- Don't change anything. Just click Save Changes.
WordPress will try to write a fresh .htaccess with the default block. If the folder isn't writable by the web server, the Permalinks screen shows you the rules to paste in by hand instead. Create a file named exactly .htaccess (leading dot, no extension) in the WordPress root, paste the block, and set its permissions to 644.
That restores permalinks. It does not restore your custom rules: redirects, security blocks, caching headers, PHP settings. Those only come back from a copy of the old file. So the full recovery order is:
- Look for the old file in your most recent backup that has one (use the
tar -tzfcheck above). - If you find it, put it back in place, then save permalinks once so the WordPress block is current.
- If you don't, check your old host's account, if it still exists. The file is probably still sitting in
public_html. - If neither works, regenerate the WordPress block, then rebuild the custom rules from memory, plugin settings, and your list of known redirects.
For redirects, there's often a partial way back. Pull the list of URLs that started returning 404 from Search Console, compare them against your old sitemap or an archived copy of the site, and rebuild the redirect map from that. It's slow work. A backup that kept the file saves you from it.
If you're moving domains rather than hosts, redirects deserve their own plan. Our guide to migrating WordPress to a new domain without losing rankings covers building that redirect map before the move, so it never depends on a single file surviving.
Nginx hosts: where the same rules live instead
If your site runs on Nginx, you won't find an .htaccess, and adding one does nothing. The WordPress handbook page on Nginx says it plainly: with Nginx "there is no directory-level configuration file like Apache's .htaccess," and all configuration happens at the server level, where WordPress can't change it.
The permalink job is done by a single directive in the site's server block:
location / {
try_files $uri $uri/ /index.php?$args;
}
That's the Nginx equivalent of the two RewriteCond lines plus the final RewriteRule: try the real file, then the real directory, then fall back to index.php.
This changes the backup question. On Nginx, your rules live in a file like /etc/nginx/sites-available/example.com or in your host's control panel, not in the WordPress folder. A backup of the WordPress folder will never contain them, however complete it is. On a managed Nginx host that's usually fine, because the host owns that config and rebuilds it for you. On a VPS you manage yourself, copy the server block somewhere safe every time you change it, and keep a written list of the redirects in it.
One more twist: plenty of hosts run Nginx in front of Apache as a reverse proxy. The handbook notes that many sites reporting Nginx in their response headers are actually Apache behind an Nginx proxy. In that setup .htaccess still works and still matters. If you're unsure, check whether your site has the file. Its presence, and the fact that permalinks break when you rename it, is a quick test.
A short restore checklist for .htaccess
Keep this somewhere you'll find it during a bad night:
- Before any migration, download a copy of the live
.htaccesson its own and keep it next to your backup. - Run
tar -tzforunzip -lagainst one backup per method you use, and confirm the root.htaccessis listed. - After a restore, test three URLs: the homepage, a deep post URL, and one old URL you know should redirect.
- If posts 404, save permalinks first, then put the old file's custom rules back.
- If you get a 500, rename the file and read the error log before editing anything.
- On a self-managed VPS, keep the Nginx server block or Apache vhost in version control or your backup set.
Backups that restore cleanly are the whole point. If you'd like backups that take the full application folder and verify every archive before calling it good, start with the 30-day free trial. It covers 1 site with 7 days of retention, no credit card needed.
Frequently Asked Questions
Where is the .htaccess file in WordPress?
It's in the WordPress root folder, the same directory that holds wp-config.php and the wp-admin folder. On cPanel hosts that's usually public_html. It's a hidden file because its name starts with a dot, so switch on "show hidden files" in your file manager or FTP client to see it.
What is the default WordPress .htaccess?
For a single-site install, it's the block between # BEGIN WordPress and # END WordPress: RewriteEngine On, an Authorization header pass-through, RewriteBase /, a rule that leaves index.php alone, two conditions that skip real files and folders, and a final rule sending everything else to /index.php. The WordPress Advanced Administration Handbook publishes the exact text, plus separate versions for multisite.
Why do my pages 404 after restoring a backup?
Usually because the restore brought back the database and wp-content but not the root .htaccess, so the rewrite rules that power pretty permalinks are missing. If the homepage loads but posts don't, go to Settings → Permalinks and click Save Changes to regenerate the default rules, then restore any custom redirects from a copy of the old file.
Does my backup include .htaccess?
Only if the tool archives root-level files and doesn't skip dotfiles. Don't assume. List the archive contents with tar -tzf backup.tar.gz | grep htaccess or unzip -l backup.zip | grep htaccess and look for the file at the WordPress root.
Can I regenerate .htaccess from wp-admin?
Yes, for the default WordPress block. Open Settings → Permalinks and click Save Changes without changing anything. If the server can't write the file, WordPress shows you the rules to paste in yourself. This won't recreate custom redirects, security rules or PHP settings; those only come back from a saved copy.