The Default WordPress .htaccess File, and Safe Things to Add
WordPress on Apache needs a short .htaccess block in the site root that sends every request for a file or folder that does not exist to index.php, which is what makes pretty permalinks work. Paste the block between # BEGIN WordPress and # END WordPress, and put your own rules above those markers.
| Line | Job |
|---|---|
| RewriteEngine On | Turns on mod_rewrite for this folder |
| RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] | Passes the Authorization header through to PHP |
| RewriteBase / | Rewrites are relative to the site root |
| RewriteRule ^index\.php$ - [L] | Requests for index.php itself are left alone |
| RewriteCond %{REQUEST_FILENAME} !-f | Only if no real file matches |
| RewriteCond %{REQUEST_FILENAME} !-d | And no real folder matches |
| RewriteRule . /index.php [L] | Hand everything else to WordPress |
Updated
A broken .htaccess usually shows up as every page except the homepage returning 404, or the whole site returning a 500 error after a plugin edit. Pick your install type below, choose the extras you want, and copy a complete file. The WordPress part matches the rules published in the WordPress developer documentation.
.htaccess builder
WordPress install type: Single site, Multisite, subfolders, Multisite, subdomains.
Extras placed above the WordPress block:
- Redirect every http:// request to https://
- Block web access to wp-config.php, .htaccess, .htpasswd and debug.log
- Stop Apache listing the contents of folders without an index file
Only turn on the HTTPS redirect once your certificate works, or visitors will hit a browser warning.
Your .htaccess file
# Block web access to sensitive files
<FilesMatch "^(wp-config\.php|\.htaccess|\.htpasswd|debug\.log)$">
Require all denied
</FilesMatch>
# 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
The three official WordPress blocks
These are the rules WordPress documents for each install type. Use exactly one of them.
Single site
The block for a normal WordPress site installed in the web root.
# 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 WordPressMultisite with subfolders
For networks where each site lives at a path such as example.com/shop/.
# BEGIN WordPress Multisite
# Using subfolder network type: https://wordpress.org/documentation/article/htaccess/#multisite
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress MultisiteMultisite with subdomains
For networks where each site has its own subdomain such as shop.example.com.
# BEGIN WordPress Multisite
# Using subdomain network type: https://wordpress.org/documentation/article/htaccess/#multisite
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress MultisiteSafe additions
Each of these goes above # BEGIN WordPress. Add one at a time and reload the site after each.
Deny web access to sensitive files
Uses the Apache 2.4 Require directive, so an older 2.2 server would need the old Order and Deny syntax instead.
# Block web access to sensitive files
<FilesMatch "^(wp-config\.php|\.htaccess|\.htpasswd|debug\.log)$">
Require all denied
</FilesMatch>Force HTTPS
A permanent 301 redirect that keeps the host and path. If your host offers an HTTPS switch in its panel, use that and skip this.
# Send http:// requests to https://
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How the rewrite works
Apache reads the conditions top to bottom. A request for a real image or stylesheet matches -f and is served directly, a request for a real folder matches -d, and anything else falls through to index.php, where WordPress looks up the post, page or archive for that address.
The HTTP_AUTHORIZATION line copies the Authorization header into a variable PHP can read. On some server setups PHP never sees that header otherwise, which breaks logins made through it.
Where your own rules go
Put redirects, HTTPS rules and access blocks above # BEGIN WordPress. Rules run in order, so a redirect placed after the WordPress block would never be reached for most URLs.
Keep additions few and tested. Each rule runs on every request, and a typo anywhere in the file takes the whole site down with a 500 error.
How it works
- Choose single site, multisite with subfolders, or multisite with subdomains.
- Tick the extras you want. Each one is placed above the WordPress markers so WordPress never overwrites it.
- Copy the file, back up your current .htaccess, and upload the new one to the folder that holds wp-config.php.
- Load a post and the admin area to confirm, then visit Settings, Permalinks and click Save to let WordPress check its rules.
Tips
- Keep a copy of the working file before any plugin touches it. Restoring a known-good file is faster than debugging a 500 error.
- If you can edit Apache's main configuration, put these rules there instead. Apache's own docs say .htaccess costs a lookup in every directory on every request.
- Never edit inside the BEGIN and END WordPress markers by hand. WordPress can rewrite that section when permalink settings are saved.
- Nginx ignores .htaccess entirely. On an Nginx host the same job is done by a try_files line in the server block.
Frequently asked questions
Where is the .htaccess file in WordPress?
In the root folder of the WordPress install, next to wp-config.php and the wp-content folder. Its name starts with a dot, so file managers and FTP clients often hide it until you turn on hidden files.
How do I restore the default WordPress .htaccess?
Replace the file's contents with the single-site block on this page, or rename the broken file and visit Settings, Permalinks in the admin and click Save. WordPress writes a fresh block when it can write to the file.
Why do I get 404 errors on every page except the homepage?
Almost always the rewrite rules are missing or mod_rewrite is off. The homepage loads because it is index.php itself; every pretty permalink needs the rule that sends unknown paths to index.php.
Which multisite block do I need?
The one matching how the network was installed. Sites at example.com/shop use the subfolder block; sites at shop.example.com use the subdomain block. The two differ in how they strip the site prefix from wp-admin and file paths.
Is it safe to block access to wp-config.php in .htaccess?
Yes. PHP reads wp-config.php from disk, not over the web, so denying web requests to it does not affect the site. The WordPress documentation suggests the same FilesMatch rule shown here.
Related: Redirect checker, Fix too many redirects, We.Inc vs WordPress, WordPress alternatives.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- WordPress robots.txt: Default Rules, Editing and Safe Examples
- www vs non-www: Pick One and Redirect the Other
- YAML Comments: How to Comment, and Comment Out, YAML
- YAML Front Matter: Metadata at the Top of a Markdown File
- YAML Multiline Strings: | and >, Explained with a Live Preview
- YAML vs JSON: Which One, and Why
- Your Connection Is Not Private: Causes and Fixes
- 400 Bad Request: What Was Wrong With the Request
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App