WordPress .htaccess: Everything You Should Know About It
parallax background

WordPress .htaccess: Everything You Should Know About It

The new website launch checklist you should go through
October 11, 2023
Awesome Looking Landing Page Designs To Check Out
October 12, 2023
The new website launch checklist you should go through
October 11, 2023
Awesome Looking Landing Page Designs To Check Out
October 12, 2023
 

If you're running WordPress on Apache Web Server, you may have come across the file called ".htaccess". While it may sound strange and obscure, .htaccess is actually one of the most useful components of the Apache server software.Today we're going to dive into what .htaccess is from a beginner's point of view, so that you, too, can reap the benefits of using it correctly. Ready to learn how to edit the .htaccess file? Let's begin.

What is the .htaccess file?

 

The .htaccess file is a hidden file that lives in the root WordPress directory and it controls the various ways in which Apache serves your WordPress site to visitors. The "ht" in .htaccess stands for "hypertext", so it is, in a nutshell, a configuration file that controls access to your site. Remember that HTTP, the protocol on which all Internet sites are based, stands for "Hypertext Transfer Protocol".

WordPress functions by using files that are found below the root WordPress folder. The .htaccess file is among the most important because it is the file that Apache uses to decide how your WordPress site is ultimately presented to the world. You should note that the file isn't used by WordPress, per se, but rather by Apache.If Apache doesn't find a .htaccess file in the WordPress root folder, it will use the default .htaccess rules usually found inside the global default configuration file at /etc/apache2/apache2.conf.The WordPress .htaccess file, specifically, contains information that is used as a rule when Apache serves your WordPress site only. This means that if you have multiple sites, you can control exactly how Apache serves each one by using a different .htaccess file for each.

The file doesn’t have a specific extension and it comes hidden by default (denoted by the full stop before the filename). In order to see it, you will need to enable the viewing of hidden files in your FTP program, file manager, or SSH connection (i.e. with the ls –la command). For basic WordPress sites, the .htaccess file is responsible for handling permalinks and doesn’t go beyond that functionality. For larger websites, the .htaccess file might have many other purposes as well. Plugins can manipulate .htaccess to enable certain features on the site, such as image compression or caching.

What is this file used for?

Simply put, the WordPress .htaccess file can be used as a tool to improve the accessibility, usability, and security of your website. The file can manipulate the manner in which Apache serves files from the root directory or other subdirectories to WordPress, which can affect the overall functionality of the site in a highly specific manner. The only problem with editing this file is that one small error could lead to the entire server malfunctioning, which will involve some downtime for the site. This is why it needs to be edited with caution and all the changes should be done knowing that there is a backup file that you can quickly revert to.

By default, the purpose of .htaccess is to control the way the permalinks are displayed. One single change to the permalink structure will immediately modify the WordPress .htaccess file, adding new instructions that the .htaccess file will send to the server. If you have not made any changes to your permalinks, .htaccess might not even be created. On some sites, this is the one and only purpose that the file has. On other sites, the file can influence many other aspects.

What should you do before making any changes?

 

Before starting to make changes to your WordPress .htaccess file, you should understand that there is a risk of ruining the website if no backup is prepared in case something goes wrong. One single mistake and the file that contains some of your site’s basic functionality will be unusable. In order to make sure that everything will go just as planned, you need to take some safety measures, presented here.

Whenever you want to edit .htaccess, exercise tremendous caution and focus on these tips:

  • Back up the website entirely to have a starting point in case something goes wrong. This way, you can revert the website back to a previous state. Another option would be using a staging site instead of your actual one and see whether the changes are what you wanted or not.
  • Test the edits before performing them directly onto the site. If you want to avoid all the effort involved in reverting the site back to normal after making an unintentional change, use your staging website to perform all the required changes and see whether they work the way they should or not.
  • Download the WordPress .htaccess file locally and edit it only afterward. If you ruin the .htaccess file you can delete the broken one and add the one that you saved on your computer to bring the site back to normal.
  • Whenever you want to edit the .htaccess file, make sure to check “Show Hidden Files” from the cPanel or other hosting control panel file manager. Consider that some web hosting companies don’t allow changing this setting directly from the cPanel and you might need permission to do it. Contact the company for support if you encounter this issue.

How can you create a WordPress .htaccess file?

Normally, .htaccess is generated by WordPress when you choose the permalink structure of your website. In other cases, the .htaccess file is not generated because of server-side limitations, such as incorrect permissions. Even though you might be in this latter situation, worry not. It’s not difficult to create the .htaccess file yourself and edit it afterward. First of all, double-check whether the file is missing, or you simply can’t see the hidden files in your WordPress root directory. If you are 100% sure that the file was not generated in the first place, you can proceed to try one of these two methods:

1. Set a new permalink structure

 

In order to do this, navigate to your WordPress settings panel and then choose Permalinks from the options. Make the required changes, save them, and .htaccess should appear in the root directory of your WordPress.

2. Create it manually (the WordPress default .htaccess file)

The first method is very easy and convenient, but it doesn’t work all the time. If your WordPress can’t seem to generate the file automatically, you will have to do it by hand. To do that, follow these simple steps:

  •       Create a regular text file in Notepad
  •       Copy and paste this code into the Notepad file:
 
# BEGIN WordPress

 <IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase / 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L] 
</IfModule>
 
# END WordPress
  •       Rename the Notepad file from .txt to .htaccess
  •       Upload the newly created file to your WordPress root directory using your cPanel File Manager. The location should be /home/yourweb/public_html/.htaccess

How can you edit the WordPress .htaccess file?

If the file is already there and you just want to edit it, the process is rather simple. As mentioned before, you can change the .htaccess file in a variety of ways to alter the features of the site. Some of the changes that you can make involve redirecting URLs, protecting images from hotlinking, keeping the website secure by blocking certain IPs from accessing your site, and more.

You can edit the file in four different ways:

  •       Through cPanel
  •       Through an FTP client
  •       Through the WordPress Dashboard
  •       Through SSH (not covered in this guide)

How can you edit it using cPanel?

You can edit the file by using cPanel, which is probably the most convenient way to do it. Remember to back up the WordPress .htaccess file before modifying it immediately after you find its location. The steps involved in editing .htaccess using cPanel are:

 

1. Log into your cPanel

2. Navigate to File Manager

3. Check the option to view the Document Root

4. Check the option to show hidden files

5. Locate the .htaccess file and right-click on it after creating a backup

6. Click Edit and make the desired changes

7. Save the file and refresh the website to check if the changes took place

How can you edit it using Filezilla or other FTP clients?

 

If the cPanel method did not work, you can skip to editing the WordPress .htaccess file using an FTP client. File Transfer Protocol clients are used to edit these types of files with a simple interface. One good example is FileZilla, which is also the most popular one among WordPress users. Just the same as in the previous case, you need to locate the file first and create a backup for it. If you can’t find the file, go back to the main menu of FileZilla and check the “Force Showing Hidden Files” option. Once you do that, you should be able to see and edit the .htaccess file.

How can you edit it using the WordPress dashboard?

 

To use the WordPress Dashboard directly, you need to log into your website as admin. If you use the Yoast SEO plugin, you must first navigate to SEO, then go to General and notice the Features option at the top of the page. Once you click it, navigate to Security and turn off the “no advanced settings for authors” setting.

 

Now, navigate to Tools and click on the File editor. This will enable you to make changes in the .htaccess file. Keep in mind that you might not see this option if file editing is disabled on your site by default.

 

For those who don’t know how to use an FTP client or that are not as tech-savvy as others, the WordPress Dashboard method is the safest. It is a built-in feature that lets you edit files located in the root directory of the CMS. This is one of the best approaches, but you can also use a plugin if you can’t handle all the steps by yourself. One of the plugins you can use to edit the WordPress .htaccess file is WP .htaccess Editor. It is free, very intuitive to use, and handles all these steps for you. Just download the plugin from the repository, activate it, and use it to edit .htaccess.

Another one to try would be Rank Math SEO which has more features than WP .htaccess Editor. Once you have it installed, navigate to General Settings and finds the option that is called Edit .htaccess. As simple as that! Another option that is quite popular is Yoast, a strong SEO plugin that enables you to do much more than just edit the file.

What can be configured using this file?

 

If you are still wondering what you can do with the WordPress .htaccess, these few bits of information should help you. Here’s a list with all the changes you can make using .htaccess to configure how Apache server serves your WordPress site:

  •    Add an extra layer of security to your WordPress Admin Area
  •    Secure any important file using .htaccess
  •    Protect folders using a password
  •    Restrict access for different wp-admins
  •    Disable the execution of PHP
  •    Restrict access to certain files on the website
  •    Control image hotlinking
  •    Increase the file upload size that is generated by WordPress
  •    Modify SSL certificates
  •    Ban IP addresses from accessing the site

FAQ on the WordPress htaccess file

What's the deal with the .htaccess file in WordPress?

Oh man, the .htaccess file? It's like the backstage pass to your WordPress site. It's a configuration file used by the Apache web server. It can control many aspects of your site, from redirects to security settings. It's powerful, but with great power comes... you know, the potential to mess things up.

Why would I need to edit my .htaccess file?

Well, there are a bunch of reasons! Maybe you want to set up some 301 redirects, improve your site's security, or tweak its performance. The .htaccess file is your go-to place for these custom configurations. But, always remember to back it up before making changes. Better safe than sorry!

How do I locate this elusive .htaccess file?

Ah, the age-old quest! It's usually found in the root directory of your WordPress installation. If you're using an FTP client or cPanel's File Manager, look for a file named .htaccess. If you can't see it, it might be hidden. Adjust your settings to view hidden files, and voila!

Can I create an .htaccess file if it's missing?

Absolutely! If it's MIA, you can create one. Just open a text editor, save an empty file as .htaccess, and upload it to the root directory of your WordPress site. But, be sure you really need to create one. Sometimes it's just hiding.

How can I secure my WordPress site using .htaccess?

Ah, the security dance! The .htaccess file can be a knight in shining armor. You can use it to restrict access to certain directories, disable directory listings, or block specific IP addresses. There are plugins that can help with this, but if you're a DIY kind of person, the .htaccess file is your playground.

What if I mess up my .htaccess file?

Been there, done that! If you mess it up, your site might go down. But don't panic! Just revert to the backup you made (you did make a backup, right?). If you didn't, remove the changes, or replace it with the default WordPress code. Breathe, and maybe grab a coffee. It'll be okay.

Can I optimize my website's speed with .htaccess?

You betcha! The .htaccess file can be used to enable GZIP compression, set up browser caching, and more. These tweaks can give your site's speed a nice little boost. But, as always, tread carefully and backup before diving in.

Are there any tools to help edit the .htaccess file?

For sure! There are plugins like "WP Htaccess Editor" that can help you edit the file directly from your WordPress dashboard. But, and I can't stress this enough, always backup before making changes. These tools are handy, but they're not foolproof.

Can I use .htaccess with non-WordPress sites?

Oh, totally! While we're chatting about WordPress, the .htaccess file isn't exclusive to it. It's an Apache thing. So, if you're running another CMS or even a static site on an Apache server, you can still use the .htaccess file to work its magic.

Final thoughts

WordPress .htaccess can have many uses, so it’s best to read up further about how your website can be customized. Learning how to edit this file and how to create it when it is missing is a must to keep your WordPress site in line with the latest trends and requirements, especially in terms of cyber security. This article should answer most of your questions related to what the WordPress .htaccess file is and what you can do to change it. Since the process is relatively simple, you shouldn’t encounter any problems. If you have any problems with specific .htaccess rules, the WordPress community support forum is the best place to ask for help.

Albert Ślusarczyk
Albert Ślusarczyk
As the co-creator of Be Theme, I am a strong believer in designing with care and patience. I pour my energy, time & knowledge into perfecting the theme for our 260,000+ customers.