How to prevent overwriting .htaccess in WordPress

Last updated December 6th, 2023 06:04

WordPress and some of its plugins add their own code to the .htaccess file for their own configuration purposes. If you also have your own code, or perhaps various redirects or rules in the file, it is possible that your own code may be overwritten under certain conditions. Therefore, today we will take a look at how to prevent this from happening and how to prevent overwriting .htaccess in WordPress. Also, how to prevent overwriting of your own code.

Why does WordPress overwrite the content of the .htaccess file?

The .htaccess configuration file for the Apache web server stores important settings for the content management system.  Here you can write various rules that define how the system or individual plugins should behave. For example, thanks to this file, URL addresses can be rewritten as so-called pretty URLs.

Data in the .htaccess file can be overwritten. For example, every time you change the structure of pretty URLs in the system. At that moment, WordPress saves new rewrite rules to the file. The same is done by some plugins that have code stored in it. Therefore, if you change the settings of such a plugin, it will automatically access the .htaccess file and modify the rules in it.

This can cause your own rules that you previously saved in the .htaccess file to be overwritten or deleted.

How to prevent .htaccess overwrite in WordPress

The good news is that you can add your own rules to the .htaccess file to prevent it from being overwritten under the above-mentioned situations.

Here are some basic rules you should avoid to prevent this issue.

Do not insert your rules between WordPress tags

Rules defined by the CMS itself are labeled as tags.

You can identify them in the file by a comment.  Comment look like “# BEGIN WORDPRESS” at the beginning of the code, and “# END WORDPRESS” at the end. All code between these tags belongs to the WordPress system. If you insert your own rules between these tags, they will be overwritten every time the CMS generates new rules. Therefore, it’s important not to place any of your own rules between these two tags.

Jak u WordPressu zamezit přepisování .htaccessu

Don’t insert your rules between plugin tags.

The same applies to plugins. Any plugin that modifies rules in the .htaccess file must mark these rules. The marking is the same, except that the name of the plugin is placed after the words BEGIN and END. For example, # BEGIN WP FASTEST CACHE and # END WP FASTEST CACHE. The above mentioned also applies here, and if you don’t want to lose your rules every time you change the cache plugin settings, do not insert any custom rules between these two tags.

Jak u WordPressu zamezit přepisování .htaccessu

Create your own tags for your custom rules

If you’re modifying rules in .htaccess, create your own section and label it accordingly. Use the tags BEGIN to mark the beginning and END to mark the end of your custom section. Here’s an example.

					# BEGIN moje vlastní pravidla
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.jirivanek\.eu$
RewriteRule ^(.*)$$1 [R=301,L]
# END moje vlastní pravidla

Here’s an example code snippet that performs automatic domain redirection from non-www to www domain. As you can see from the example, I have rules marked with my own comment (a comment in the .htaccess file is created using the # symbol – the ALT + X shortcut). By creating my own section with rules, separated from sections created by the system or plugins, I can ensure that this section will not be overwritten when making any changes to the WordPress administration settings.

How to prevent overwriting .htaccess in WordPress by changing FTP permissions

Another way to accomplish this is by changing the file permissions via FTP. Each file on a Linux server has permissions that dictate which user is allowed to read or modify the file. By default, a file has permissions set to 755. If you log in to FTP, you can change the permissions on the .htaccess file to 444, which will prevent WordPress or plugins from modifying the file. If you later want to allow WordPress to overwrite the file for any reason, you can simply restore the file permissions back to 755 in the same way.

How to prevent overwriting .htaccess in WordPress

Prohibition of file rewriting through wp-config.php

The last way to avoid rewriting is by modifying the WordPress configuration file using the following code:”

					add_filter('got_rewrite', '__return_false');

Log in to your FTP, locate the wp-config.php file in the root directory of your website, and manually insert the above-mentioned line into it. Once you save the file, WordPress will no longer make any changes to it. It is important to keep in mind that this means any changes to the URL format, etc. will also not be allowed as WordPress will not have permission to make any modifications to the file.

How to prevent overwriting .htaccess in WordPress. Conclusion.

As you have learned, there are several ways to prevent WordPress from overwriting .htaccess. The easiest option is likely the first, which involves creating your own sections and inserting your rules in the appropriate places. This will allow you to maintain WordPress’s automatic functions while also avoiding the risk of losing your sometimes heavily written rules when any settings are changed. However, the choice of method is entirely up to you.

More content about WordPress

Whole category is here

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 5 / 5. Počet hlasování: 1

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.


Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Upozornit na
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje