Every WordPress installation comes with core files that determine how your website works. The .htaccess file is one of the most important WordPress files, but what does it actually do?
The WordPress .htaccess file is all about your site’s configuration. It lets you control redirects, website access, language options, and more. Essentially, it differentiates your site from every other WordPress installation running on the same Apache web server.
In this post, we’ll take a closer look at the .htaccess file. Then, we’ll show you how to locate and edit the file. Let’s jump in!
An Introduction to the WordPress .htaccess File
WordPress is a beginner-friendly platform. Most users can go about creating and managing websites without needing to edit core files.
That said, there are certain tasks that require access to these files. In particular, .htaccess (which stands for Hypertext Access) is the key configuration file, and it determines how the server runs your website.
This enables you to differentiate your site settings from every other website that’s hosted on the Apache web server. For WordPress websites, the file also contains instructions for setting your permalink structure.
You might need to use the .htaccess file to carry out the following tasks:
- Implement redirects
- Force HTTPS (if you’ve recently installed an SSL certificate)
- Change the WordPress permalink structure
- Disable resource hotlinking
- Restrict access to your website (e.g. set up password protection, IP whitelisting/blocklisting)
- Change the default language
As you can see, the WordPress .htaccess file is super important since it enables you to configure server-level rules. Although you don’t need extensive coding experience to edit the file, you’ll need to be comfortable accessing your site files and using code snippets.
We recommend proceeding with extreme caution. As one of the most sensitive files, a simple error could break your site.
The good news is that if you’re worried about editing the file manually, you can find plugins that simplify the process.
Where Is the .htaccess File Located?
The .htaccess file is included in your WordPress installation by default. You’ll find the file in your website’s root directory.
This directory contains all the files and folders needed to run your website including wp-config.php and wp-admin. It also stores all your themes, plugins, and media files.
Usually, the WordPress root directory is labeled public_html. Depending on your web hosting provider, it may be called something different such as htdocs or www. It might even contain your website’s name.
Note that you can’t access your website’s root folder via the WordPress dashboard. Instead, you’ll need to use your web host’s File Manager. Or, you can connect to your website files using an FTP client.
We’ll discuss these options in the next section.
How to Edit the WordPress .htaccess File (3 Methods)
Before we show you how to edit the WordPress .htaccess file, we recommend making a fresh backup of your website. This way, if you encounter any errors, you can easily restore a previous version of the file.
1. Use File Manager
The simplest way to edit the .htaccess file is to use the File Manager in your web hosting account.
However, this is only available if your web hosting provider gives you access to cPanel (or a custom hosting panel).
For instance, all NameHero plans include cPanel access, which makes it easy to manage files, folders, and directories. Certain plans also come with free domain names, SSL certificates, email accounts, and advanced security features:
Once you’ve opened File Manager, you need to locate the WordPress root directory, which is usually labeled public_html. The .htaccess file should be listed here.
If you can’t see it, check that your settings are configured so that you can view hidden files. In cPanel, you’ll need to go to Settings > Preferences. Then,check the box to Show Hidden Files (dotfiles) and save the changes.
Once it’s visible, right-click on .htaccess to edit the file. It’s often best to download the file to your computer to make changes within your preferred text editor. Then, you can upload the modified file back to your website using FTP (which we’ll discuss next).
2. Use an FTP Client
Another way to edit the .htaccess file is to use an FTP client.
This process is similar to the method above. However, it’s a great alternative if your web hosting provider doesn’t give you access to your WordPress website files (via cPanel or something similar).
There are plenty of FTP clients available, but FileZilla is a good choice since it’s open-source and free. Once you’ve installed and launched the software, you’ll notice that you’ll need to supply FTP credentials to connect to your WordPress site.
You should have received these details via email when you signed up for a hosting account. Otherwise, you’ll find them in your hosting account’s control panel.
Once you’ve entered the relevant details, you should be able to establish a connection to your server:
At this point, you’ll see all your local files to the left of the interface. Meanwhile, your WordPress files and folders should be visible to the right. Again, you’re looking for your site’s root directory to locate the WordPress .htaccess file.
Once you find the .htaccess file, right-click to view/edit the file. Here, you can add your code snippets anywhere before the “# END WordPress” line. You can also include comments that specify the function of each code snippet so you remember what they do. Then, simply save the changes.
3. Use a WordPress Plugin
If you lack technical experience, you might be worried about accessing the WordPress configuration file via FTP or File Manager. If that’s the case, you might prefer to use a plugin that lets you edit the .htaccess file from the WordPress dashboard.
With a plugin like HTaccess File Editor, you’ll also be able to test edits before saving them. Since this plugin is free, you can install it directly within the WordPress admin panel by going to Plugins > Add New Plugin.
Use the search feature to find “HTaccess File Editor”. Then, click on Install Now > Activate:
Now, go to Settings > WP HTaccess Editor. The plugin will notify you that it will create an automatic backup each time you make a change. You’ll have to acknowledge this notice to enable the editor:
Here, you can create a new line before “# END WordPress” and add your code snippets to set custom configuration rules.
How to Create a .htaccess File in WordPress
While WordPress should come with a .htaccess file, this might not be the case if there’s an issue with file permissions. In this instance, you’ll need to create your own WordPress .htaccess file.
To get started, navigate to Settings > Permalinks within the WordPress admin dashboard. Then, without making any changes, simply scroll to the bottom and click on Save:
This will trigger a new .htaccess file. If it doesn’t work, you’ll see an error message, which means you’ll need to manually create a WordPress .htaccess file.
To do this, you’ll need to open your site’s root directory, which means you’ll have to use File Manager (as we discussed above). Then, you should be able to add a new file from the toolbar at the top of the page.
At this point, name the file “.htaccess” and click on Create New File. Now, right-click on the file to edit it and add the following code:
# BEGIN WordPress
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]
# END WordPress
This is the default code for .htaccess files, so it should work for all websites (unless you’re using a WordPress Multisite installation). Then, simply save and close the file.
Conclusion
The WordPress .htaccess file is a powerful configuration file that’s usually available in every WordPress installation (via the root directory). Since it contains key configuration options, you can use it to add redirects, change permalink structures, password-protect directories, and more.
To recap, here are three ways to edit the .htaccess file for your WordPress website:
- Use File Manager.
- Use an FTP client.
- Use a WordPress plugin.
Do you have any questions about how to write rules for the .htaccess file? Let us know in the comments below!
Sophia is a staff writer at WordCandy.co, where she produces quality blog content for WordPress plugin and theme developers, hosting providers, website development and design agencies, and other online businesses.
Leave a Reply