It is a common issue that many WordPress users have experienced: WordPress unintentionally overwrites or edits your custom .htaccess file, either by itself or through its plugins.
This generally happens unnoticed, with no immediate impact on your site’s content, until something starts to go wrong or an unwanted change takes effect.
By default, WordPress, depending on the configuration and permission settings, can automatically edit or overwrite the .htaccess file silently. Even WordPress plugins tend to modify the .htaccess file to match their configurations.
While this is generally harmless, it can become problematic when third-party plugins or themes add code that overrides core website settings, potentially leading to security risks or performance issues.
To help you prevent such situations, we will guide you on how to stop the .htaccess file from being overwritten. So without further delay, let’s dive in.
How does the .htaccess file work and why does Wpoven even overwrite the content of the .htaccess file?
The best way to prevent WordPress or the plugins from overwriting the .htaccess file is to learn How the .htaccess file works first. It will help you figure out the root cause of the issue and understand what not to do.
So, .htaccess files are like a rule book that contains a set of instructions for how your website should behave.
Its main purpose is to manage how the content should be displayed and how certain plugins operate. For example, from the .htaccess file, you can also set the 301 redirection rule, which makes URLs SEO-friendly and clean.
But if you or someone else tries to update the website, such as changing the URL structure, WordPress will automatically also update the .htaccess file to reflect those changes.
Similarly, when you install WordPress plugins that you might use to add extra features to your site can also add their own rules/instructions to the file.
But this is not the problem, the tricky part is that when these changes happen, the original instructions you put in might get overwritten, replaced, or erased.
This could mess up how your website works or looks, even if you didn’t intend for that to happen. So, to stop or prevent such scenarios you have to follow some tips/instructions mentioned below:
How to stop WordPress from overwriting the .htacess file’s rules?
Since the .htaccess
file contains rules or instructions based on which your website works, it can also have a custom set of rules added to it to stop it from being overwritten in the above scenarios.
Here’s how you can do it.
1. Avoid adding or editing rules between the WordPress markers or Tags
As we have already mentioned, WordPress uses a .htaccess file to manage certain WordPress settings like creating structured and clean SEO-friendly URLs.
When you change the URLs structes which are also called “permalinks”, WordPress automatically updates the code in this file as well.
These permalink settings are saved between two WordPress tags or markers: # BEGIN WordPress
and # END WordPress
.
This particular area between these two WordPress tags is completely reserved for WordPress and also it warns you that any changes you make between these markers will be overwritten the next time WordPress updates the file.
Dos: But the best part is, that WordPress won’t touch any code you add outside of these markers or tags. It means, that if you want to add any custom code, try to insert or add it at the top of the file, before the # BEGIN WordPress
marker.
Dont: But avoid inserting or adding your custom code between the WordPress markers because it will be deleted or replaced whenever WordPress updates its settings. To keep your custom settings safe, always place them outside these markers.
2. Avoid adding or editing rules between the Plugin markers or Tags
Just like WordPress markers, you are also required to avoid editing or adding rules between the plugin markers or tags.
This is because plugins can also add their own rules to the .htaccess
file similar to WordPress and when they do, they reserve the area with special markers or tags that include the plugin’s name.
For example, if a plugin called “WProcket” adds rules, it will mark them with # BEGIN WP Rocke
t Browser Cache and # END WP Rocket
Browser Cache.
It means, that any instruction or rules the plugin adds between these two markers are fully controlled by the plugin only. In case, you change the plugin’s settings, it will update these rules and potentially overwrite anything between those markers or tags.
So, if you have any custom rules that you don’t want to lose, don’t place them between these plugin-specific markers.
Add custom rules using custom markers or tags
Instead, you can create markers or tags to add any custom rules you have and keep them separated from the sections generated by WordPress and plugins.
This method will guarantee that any edits or changes made to the WordPress administration or Plugin settings do not overwrite this section.
Here’s how you can do it:
Suppose you need to redirect any request made yourdomain.com
to https://www.yourdomain.com
, so that your visitors always land on the version of your site with “www” in the URL.
To implement this custom rule, use the tags # BEGIN Custom Rules
# END Custom Rules
at the top of the .htaccess
file. Then, add your instructions or custom rules within these markers.
It will be somewhat like this:
# BEGIN Custom Rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
# END Custom Rules
Read: 🚩 WordPress .htaccess File: How to Create And Edit it?
3. Set file permission to avoid WordPress from overwriting the .htaccess file
The third method that can help you prevent WordPress from overwriting the .htaccess
file is to set the file permissions to “read-only.”
To implement this, you need to access the .htaccess
file using either an FTP client or the file manager provided by your WordPress hosting provider.
Once you’re logged into the FTP client or file manager, look for the .htaccess
file, which is most likely located in the root directory of your WordPress installation.
Then, right-click on the file and select ‘File Permissions.’
A pop-up window will appear with all the file attributes, in which the default setting is typically 644, but you can change this to 444 by either entering the value directly into the ‘Numeric value’ field or ensuring that only the ‘Read’ boxes are checked.
By setting the .htaccess
file permissions to 444 (read-only), the file will be protected from being modified.
Note: Always Keep in mind that this will also disallow you from making any changes to the file. If you need to update the file, you’ll have to temporarily change the permissions back to 644.
4. Stop WordPress from overwriting the .htacess by inserting code into the wp-config.php file
The last method you can implement is by inserting the following code snippet in your wp-config.php file.
add_filter('got_rewrite', '__return_false');
This approach is useful if you want to prevent WordPress from modifying the .htaccess
file without affecting your plugins.
Note: Editing or handling core WordPress files can be risky and may cause potential damage to your website if anything goes wrong. It’s advisable to seek assistance from an expert or contact your web developer.
Now, to implement this,
1. Log in to your cPanel or FTP using your credentials.
2. Navigate to the root directory of your website and find the wp-config.php
file.
3. Open the wp-config.php
file for editing.
4. Manually add the following line to the file as shown in the picture
add_filter('got_rewrite', '__return_false');
5. After adding the code, save the file.
6. Once saved, WordPress will no longer be able to make any modifications to the file, including changes to the URL format or other related settings.
Once you follow this method, WordPress will not be able to make any modifications or overwrite the .htaccess file.
Conclusion
It is always said that prevention is better than cure. Similarly, you can take preventive measures such as avoiding adding or editing rules between the WordPress markers or tags, as well as between the WordPress plugin markers and tags.
Additionally, you can set up some rules and use the read-only method to completely disallow any modifications to the .htaccess
file.
We hope the above methods will help you stop WordPress from overwriting the .htaccess
file. If you have any queries regarding this post, please let us know in the comment section below.
Frequently Asked Questions
Will my customized .htaccess rules be impacted by WordPress upgrade processes?
No, your customized .htaccess
rules will not be impacted by WordPress upgrades if they are placed outside the # BEGIN WordPress
and # END WordPress
markers. WordPress only modifies the code between these markers.
Are there any disadvantages to using portions of custom .htaccess?
Yes, Custom .htaccess
rules can introduce complexity, potentially leading to conflicts or errors. If you do not properly configure the rules, it might also affect your site’s performance or cause server issues. Additionally, these rules may require updates if server configurations, plugins, or WordPress itself are changed. There are also security risks, as incorrect rules can inadvertently create vulnerabilities if not carefully managed.
How can I make sure that when I update a plugin, my personalized .htaccess rules don’t get lost?
To keep your custom .htaccess
rules safe when updating plugins, make sure to place your rules outside the # BEGIN
and # END
markers that WordPress and plugins use. You can also create your own markers to keep your rules separated. Regularly back up your .htaccess
file so you can easily restore it if needed. After updating plugins, check the .htaccess
file to ensure your custom rules are still there and haven’t been lost.
Rahul Kumar is a web enthusiast, and content strategist specializing in WordPress & web hosting. With years of experience and a commitment to staying up-to-date with industry trends, he creates effective online strategies that drive traffic, boosts engagement, and increase conversions. Rahul’s attention to detail and ability to craft compelling content makes him a valuable asset to any brand looking to improve its online presence.