BEGIN WordPress

How to Stop WordPress From Overwriting Your .htaccess File (2023 Guide)

If you‘ve ever added custom rules or settings to your WordPress site‘s .htaccess file, you may have noticed that sometimes WordPress overwrites your changes. This can be incredibly frustrating, especially if you spent time carefully crafting custom .htaccess rules to improve your site‘s performance, security, or SEO.

In this in-depth guide, we‘ll explain exactly why WordPress overwrites the .htaccess file and show you how to prevent it from happening. Whether you want WordPress to leave your custom .htaccess rules alone or prevent it from writing to .htaccess entirely, you‘ll learn multiple ways to accomplish your goal.

Let‘s start with a quick overview of what the .htaccess file is and why it matters for your WordPress site.

What Is the .htaccess File?

The .htaccess file is a special configuration file used by Apache web servers. It allows you to specify configuration settings and rules on a directory-by-directory basis.

Some common uses for .htaccess include:

  • Rewriting and redirecting URLs
  • Specifying custom error pages
  • Configuring caching and compression
  • Restricting access to directories
  • Improving security by blocking malicious bots and attacks

In the context of WordPress, the .htaccess file has a few important roles:

  1. It‘s used by WordPress to store your permalink settings. This is what allows you to use pretty permalinks like /sample-post/ instead of ugly default links like /?p=123.

  2. Some WordPress plugins write their own custom rules to .htaccess. Caching plugins, security plugins, and SEO plugins often do this to apply sitewide optimizations.

  3. WordPress developers and power users can add their own custom .htaccess rules. You might do this to optimize performance, beef up security, or enable other custom functionality.

By default, WordPress automatically writes certain rules and configuration settings to the .htaccess file. The core WordPress software does this to manage your permalink settings. Some plugins also automatically write their own rules to .htaccess.

This automation is convenient, but it can cause problems if you‘ve added your own custom rules to the file. You may come back and find that WordPress or a plugin has overwritten your custom configuration!

That‘s understandably frustrating. The good news is that by understanding a bit more about how WordPress uses the .htaccess file, you can safely make your own modifications without them getting erased. Even better, you can stop WordPress from writing to .htaccess at all if you prefer.

How to Add Custom Rules to .htaccess Without WordPress Overwriting Them

The first thing to understand is that WordPress and most well-behaved plugins will only overwrite certain sections of the .htaccess file.

Specifically, WordPress itself only overwrites the code between these two markers:

Any rules between those two lines are considered fair game for WordPress to overwrite, usually when you update your permalink settings.

Similarly, most plugins will also mark off their rules, like this:

So for example, if you‘re using the popular W3 Total Cache plugin, you‘ll see sections like:

The plugin knows that it "owns" the rules between those lines and can overwrite them as needed.

The key takeaway is this: as long as you add your own custom rules outside of those marked sections, they won‘t get overwritten by WordPress or well-behaved plugins.

Where exactly should you put your custom rules? In general, the top of the .htaccess file before the # BEGIN WordPress line is a good spot. You can also put them at the very bottom of the file.

Here‘s what that might look like:

<Files .htaccess>
order allow,deny
deny from all

This snippet is just an example. It prevents direct access to the .htaccess file itself as a security precaution.

The important thing is that it‘s outside of the WordPress section, so those rules won‘t get overwritten when WordPress updates the file.

Use Custom Markers for Your Own Rules

What about badly behaved plugins that might overwrite rules outside of their marked territory? Or what if you want to add custom rules to the middle of the file for some reason?

In that case, you can create your own BEGIN/END markers:

Anything between those lines should be considered hands-off for WordPress and plugins. They likely won‘t overwrite code between custom markers.

You might use a section like this to apply custom rewrite rules, security settings, redirects, etc.

Making .htaccess Read-Only

If you want to be 100% certain that WordPress won‘t overwrite your custom .htaccess rules, you can make the file read-only. This means WordPress and plugins can read the file but can‘t write to it.

Here‘s how:

  1. Connect to your site via FTP/SFTP or open the File Manager in your hosting control panel.

  2. Navigate to your WordPress site‘s root directory (usually public_html or www).

  3. Find the .htaccess file, right-click it, and choose "File Permissions."

  4. Change the file permissions to 444. This makes it read-only.

  5. Save the changes.

Now WordPress won‘t be able to write to the .htaccess file at all. Keep in mind that this means you also won‘t be able to update your permalink settings via the WordPress admin area. If you change your permalink structure, you‘ll need to manually update the .htaccess file yourself.

To do that, set the permissions back to 644 temporarily. Then edit the file and look for these lines:

RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Update the RewriteBase line if needed, then save the file and set the permissions back to 444.

One other thing to note: some plugins may behave unexpectedly or throw errors if they‘re unable to write to .htaccess. For example, caching plugins that apply sitewide optimizations via .htaccess rules. You may need to make .htaccess writable again to use those plugins properly.

Preventing WordPress from Writing to .htaccess with Code

If you‘d rather keep .htaccess writable but prevent WordPress from making changes to it, you can do so with a small code snippet. This is a good option if you want plugins to be able to use .htaccess while still stopping WordPress from overwriting your custom permalink rules.

Here‘s the code to use:

add_filter(‘got_rewrite‘, ‘__return_false‘);

You can either add this to your theme‘s functions.php file or use a plugin like Code Snippets to run it.

With this filter in place, WordPress will no longer update the .htaccess file with your permalink settings. Instead, you‘ll need to add the relevant rewrite rules manually.

When you save your permalink settings, WordPress will show an error message telling you to update your .htaccess file. It will even show you the exact rules you need to add:

RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

To make the update, edit your .htaccess file and replace everything between the # BEGIN WordPress and # END WordPress markers with those rules.

Again, keep in mind that you‘ll have to repeat this process any time you change your permalink structure. WordPress won‘t be able to update .htaccess automatically.

Troubleshooting .htaccess Issues

Hopefully you now have a solid understanding of how WordPress uses the .htaccess file and how you can safely add your own custom rules. But working with .htaccess can still be tricky, especially for beginners.

Here are a few common issues and how to resolve them:

  • 500 Internal Server Error – This usually means there‘s a syntax error in your .htaccess file. Try undoing your recent changes or restoring a backup of the file.

  • Changes not taking effect – Browser and server caching can interfere with .htaccess changes. Clear your browser cache and consider adding some rules to .htaccess itself to control caching.

  • Accidentally locking yourself out – If you use .htaccess to restrict access to wp-admin but make a mistake in the rules, you might lock yourself out of the dashboard. To regain access, you‘ll need to use FTP or your hosting file manager to edit .htaccess and fix the rule.

  • Redirect loops – Creating a rewrite rule that ends up redirecting infinitely will bring down your whole site. Double and triple check your rewrite and redirect rules before deploying them.

When in doubt, make a backup of your .htaccess file before making changes. That way you can always roll back if something goes wrong.

It‘s also a good idea to test your changes on a staging site before deploying to production. Some managed WordPress hosts like WP Engine and Flywheel make staging effortless.

Useful .htaccess Guides and Resources

Want to learn more about .htaccess and how you can use it to improve your WordPress site? Check out these handy guides and resources:

  • The Ultimate Guide to .htaccess for WordPress
  • 10 .htaccess Hacks to Secure & Optimize Your WordPress Site
  • How to Set Up Redirects in WordPress Using .htaccess
  • Stupid htaccess Tricks (Smashing Magazine)
  • Apache HTTP Server Documentation – .htaccess files

With a deeper understanding of .htaccess and a few WordPress-specific best practices, you‘ll be able to add custom rules and settings without worrying about WordPress overwriting your changes.

Did you like this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.