The WordPress .htaccess file is a plain text file that tells an Apache web server how to handle requests to your site. WordPress writes its permalink rules there, and you can add rules for redirects, security and caching.
We tested everything here in October 2026 on a WordPress 7.1.3 test site running Apache 2.4.67 and PHP-FPM. We broke the file on purpose, restored it, and checked each rule with curl. We did not test a hosting File Manager, Nginx or LiteSpeed, so those parts come from vendor docs.
This guide explains what the file does and where to find it. It shows how to create and edit it safely, then covers seven rules and the reasons a rule fails.
What the WordPress .htaccess File Does
The .htaccess file is a per-directory configuration file that Apache reads on every request. WordPress uses it to route pretty permalink URLs to index.php, which is why posts return 404 errors without it. You can also add rules for redirects, access control, caching and compression on the same server.
The WordPress developer docs describe it as a distributed configuration file for Apache. They also say WordPress edits it to handle pretty permalinks.
Apache’s own guide says to put settings in the main server config when you can. It adds that httpd checks every parent directory for the file on each request and recompiles regular expressions every time. For a file four levels deep, the guide counts four extra filesystem lookups per request.
The same guide says .htaccess fits cases where you lack root access, such as managed hosting. That describes most WordPress sites, which is why this file matters.
Multisite networks use a different block. The WordPress docs list separate rules for subfolder and subdomain networks, so do not paste the single-site block into a network.
Only Apache and compatible servers read it. Nginx ignores the file, and the WordPress docs cover Nginx rules on a separate page.
Every plugin that writes rules into this file is one more thing to check when an error appears. Plugin load matters here too, which we cover in are 50 plugins too much for your WordPress eShop.
Where Is the .htaccess File in WordPress?
The WordPress .htaccess file sits in the root folder of your install, next to wp-config.php and the wp-admin folder. Its name starts with a dot, so file managers and FTP clients hide it by default. Turn on the hidden files option in your client or File Manager to see it.
On our test site, a directory listing with hidden files showed .htaccess beside index.php and wp-admin. WordPress had written it with a mode of 644 and a size of 523 bytes.
Other folders can hold their own .htaccess files. Apache applies each file to its own directory and everything below it, and our uploads folder already held an empty one.

This is the default block WordPress 7.1.3 wrote on our site:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
Those are the permalink rules, built on the mod_rewrite module. The two RewriteCond lines skip real files and folders, and the last RewriteRule sends every other request to index.php. The comments say anything between the markers will be overwritten.
If you cannot find the file, hidden files are probably off, or your host runs Nginx. If you would rather avoid server files entirely, our comparison of the best WordPress AI website builders covers hosted options.
How to Create a .htaccess File for WordPress
To create a .htaccess file for WordPress, open Settings, then Permalinks, and click Save Changes. WordPress writes the file for you when the site folder is writable, and a hand-made copy works the same way. Both routes gave us working pretty permalinks on the test site within seconds.
Step 1: Confirm the File Is Missing
We deleted .htaccess and requested the sample page. Apache returned 404, because nothing was sending the pretty URL to index.php.

Step 2: Save the Permalinks Screen
Go to Settings, then Permalinks, and click Save Changes without changing anything. Our guide to setting up pretty permalinks in WordPress covers the structure choices on that screen.

WordPress wrote a fresh .htaccess file with the default block, and the sample page returned 200 again.

Step 3: Create the File by Hand
If the folder is read-only, create the file yourself with the default block. The WordPress hardening guide says .htaccess in the root can be writable by the web server if you want WordPress to generate rules.

When you save the file on your computer, make sure the editor does not add a .txt extension. Upload it to the same folder as wp-config.php.
Tip: Keep the file at mode 644, which is what WordPress used on our test site.
How to Edit the WordPress .htaccess File Safely
To edit htaccess safely, back up the file first, then open it and add your rules outside the WordPress block. Test the site, and restore the backup if anything breaks. A mistake on a live site returns a 500 error.
Step 1: Back Up the File
Copy the file to .htaccess.bak before you change anything. A backup is the fastest fix for a 500 error, because you can restore it in seconds.

Step 2: Open the File
You have three ways in: an SFTP client, your host’s File Manager or an SSH editor. Our guide to using FileZilla covers the FTP route.
In cPanel, the File Manager docs say to select Show Hidden Files (dotfiles) in Settings. We edited over the command line, and we did not test FTP or File Manager because both need a hosting account.

Step 3: Add Rules Outside the WordPress Block
Place your rules above the line that reads BEGIN WordPress, or below END WordPress. WordPress rewrites everything between the markers whenever it flushes permalink rules.
We tested it. We added one comment above the block and one inside it, then clicked Save Changes on the Permalinks screen. The inside line vanished, and the line above survived.


Step 4: Test Right Away
Reload the home page, one pretty permalink and the login page. Also test with curl or a private window, because a browser can keep serving an old redirect.

Changes took effect on the next request. We never restarted Apache.
Worth checking: If a 500 error also hides wp-admin, restore the backup over SFTP or SSH. You cannot reach the dashboard to fix it from there.
Step 5: Restore the Backup if the Site Breaks
We added one invalid line and the home page returned 500. The Apache error log named the problem, and restoring the backup brought the site back with a 200.

Step 6: Delete the Backup Copy
Remove .htaccess.bak once the site is stable. Leftover copies add clutter, and an integrity check we ran on the same site flagged a stray .htaccess.bk file in the root.


7 Useful .htaccess Rules for WordPress
These seven rules handle the most common WordPress jobs, from HTTPS redirects to compression. We checked each one with curl before and after, and each result is shown below. Place every rule above the WordPress block so a permalink save cannot overwrite it.
Many htaccess WordPress guides list dozens of rules, so we kept to seven. These WordPress htaccess rules assume Apache 2.4. A rule that needs a module your server lacks will return an error, so watch the log.
1. Redirect HTTP to HTTPS
This rule sends every HTTP request to the same address over HTTPS with a 301 redirect:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It worked on our site: curl got a 301 with a Location header pointing at https. Behind a proxy that ends TLS, the same rule redirected a request that already carried an HTTPS header, which is how a redirect loop starts.
For that setup, test the forwarded header instead:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
With this version, our request with the HTTPS header returned 200. If you already see a loop, our guide to fixing the ERR_TOO_MANY_REDIRECTS error explains the usual causes.

2. Redirect an Old Page
A single Redirect line sends an old URL to a new one:
Redirect 301 /old-page/ /sample-page/
Before the rule, /old-page/ returned 404. After it, curl showed a 301 with a Location pointing at the sample page. For plugin-based options, see how to redirect a page or URL in WordPress.

3. Protect wp-config.php
The WordPress hardening guide gives this deny rule for wp-config.php. It uses the Apache 2.4 syntax:
<Files wp-config.php> Require all denied </Files>
Before the rule, a request for wp-config.php returned 200. After it, the same request returned 403. The file is still readable by WordPress itself, because PHP reads it from disk.

4. Turn Off Directory Listing
One line stops Apache from showing a folder’s file list when no index file exists:
Options -Indexes
We switched listings on in our server config first. The uploads folder then returned a page titled Index of /wp-content/uploads with status 200. After the rule, it returned 403.

5. Block xmlrpc.php
A Files block can deny the XML-RPC endpoint in the same way:
<Files xmlrpc.php> Require all denied </Files>
A GET request to xmlrpc.php returned 405 before the rule and 403 after it. Some apps and plugins use XML-RPC, so check what depends on it before you block it.

6. Add Browser Caching Headers
The mod_expires module sets how long browsers keep a file:
<IfModule mod_expires.c> ExpiresActive On ExpiresByType text/css "access plus 1 month" ExpiresByType image/webp "access plus 1 year" </IfModule>
Before the rule, a WooCommerce CSS file came back with no caching headers. After it, the response carried Cache-Control with max-age=2592000, which is 30 days, plus an Expires date.

7. Compress Text Files
The mod_deflate module compresses text files before they leave the server:
<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/css application/javascript text/html </IfModule>
The same WooCommerce CSS file was 384,979 bytes when we asked for gzip without the rule. With the rule it was 43,321 bytes, about 89% smaller.

Why a .htaccess Rule Does Nothing or Returns a 500 Error
A .htaccess rule fails for three reasons: Apache is ignoring the file, the rule needs a directive your server lacks, or the syntax is wrong. The first gives no error. The other two usually return a 500, and the Apache error log names the cause.
Apache’s guide says AllowOverride defaults to None, which turns .htaccess processing off. It also says a test directive that produces no 500 suggests AllowOverride None is in effect.
We confirmed it. With AllowOverride All, an invalid line returned 500. After we set AllowOverride None and reloaded Apache, the same file returned 200 and every rule was ignored.

PHP settings are a common trap. Old guides tell you to add php_value lines, but they only work when PHP runs as an Apache module. On our PHP-FPM site, php_value returned 500 and the log said the command was not defined by any module.

Your host panel or an SSH session gives access to the Apache error log. Ours printed one line per failed request, and the line named the exact directive. Read it before you guess.
Ask your host which PHP setup you have before you add PHP directives. A wrong directive returns a 500 as our test showed, so try any AI-written rule on a copy first. For other uses of AI on a site, see our guide to AI integration with WordPress.
A 403 after adding rules is a different problem, and our guide to fixing the 403 forbidden error covers it. If you would rather not risk server files on a live store, our WordPress development services are one option.
Conclusion
The WordPress .htaccess file routes permalinks and carries your redirects, deny rules and caching headers. Back it up, add your own rules above the WordPress block and test every change before you leave the page.
On our test site, a rule inside the block disappeared after one permalink save. A single invalid line returned a 500 until we restored the copy. Those two results explain most .htaccess problems.
Frequently Asked Questions (FAQs)
Q1. Can I delete the .htaccess file in WordPress?
Yes. WordPress writes a new one when you save the Permalinks screen. On our test site, pretty URLs returned 404 until we did that, and then they returned 200.
Q2. Why can’t I see .htaccess in my File Manager or FTP client?
The name starts with a dot, so many tools hide it. Turn on the hidden files option, such as Show Hidden Files in cPanel. If it is still missing, your host may run Nginx.
Q3. Do .htaccess changes need a server restart?
No. Apache reads the file on every request, so each change worked on the next page load in our tests. Only edits to the main server config need a reload.
Q4. What file permissions should .htaccess have?
WordPress set 644 on our test site. The WordPress hardening guide says the root .htaccess can be writable by the web server if you want WordPress to generate rewrite rules.
Q5. Can visitors read my .htaccess file?
They should not. A standard Apache setup denies web access to files starting with .ht. Our test server returned 403 for /.htaccess after we added that standard block.
Q6. Can attackers change my .htaccess file?
Yes. The WordPress.org hacked-site guide calls it one of the most common files attackers change. Check it first if you see odd redirects, and compare it with the default block.
Q7. Where do plugins write their own rules?
Many caching and security plugins add marked blocks to the same file. Leave their blocks alone, and place your own rules above the WordPress block or below its END line.
Q8. How do I check which .htaccess rules are working?
Request a URL with curl and read the status and headers, as we did for each rule above. If nothing changes, check the Apache error log and confirm AllowOverride is not set to None. Add one rule at a time so a failure points at one line.
