.htaccess is one of the most important files for a WordPress site. This file controls the server and offers lots of additional features to manage the performance, behavior, and security of your WordPress site.
Since the file plays such a critical role, you should have a clear understanding of how to deal with it. In today’s post, I will show you how to create and edit the .htaccess file.
Are you ready? Let’s get started.
What is the .htaccess File?
First of all, let’s take a minute to understand what the .htaccess file is. In its basic form, .htaccess is a configuration file that instructs your server to perform various tasks like –
- Protect your site with a password
- Redirect visitors to another URL
- Deny access based on specific IP addresses
- Customize the error pages
- Prevent image hotlinking
- Change the default loading page
Some of you may wonder why there’s a dot in front of the file name. Putting the dot indicates that it is a hidden file. That’s why you won’t find the file in the file manager or FTP client, unless you enable the view hidden files option.
How to Create the .htaccess File?
If you can’t find the .htaccess file in the file manager even after enabling the “Show hidden files” option, you have to create the file manually. WordPress automatically creates the file when you change the permalink structure. If you are using the default permalink structure, you have to create it manually.
To create the .htaccess file, log in to your WordPress dashboard. Go to Settings > Permalinks. Click the “Save changes” button. At this point, WordPress will create the .htaccess file for you.
On some occasions, your server’s file permission settings might prevent WordPress from creating the .htaccess file. In that case, you have to create and upload the file manually.
If you are using any other permalink structure than the default one, you will get some codes to put into the .htaccess file. Copy the code and proceed to the next section where I show you how to create and upload the .htaccess file on your own.
How to Create and Upload the .htaccess File Manually
Creating the .htaccess file is very simple. Just open up your favorite text editor, paste the code you copied a while ago, and save the file as “.htaccess” in your computer. That’s it, you just have created the .htacces file.
At this point, the file contains only the permalink structure information. If you want to use other features like adding a password to the admin section, setting up redirection, prevent hotlinking, etc., you have to add separate code.
There are several tools for generating the code, including the .htaccess Generator. This online tool enables you to generate the necessary code for complete site redirection, 301 redirection, password-protecting specific pages, preventing hotlinking, using custom error pages, blocking IP addresses, etc.
Once you have got the code, add the code to the .htaccess file you created. Now, you have to upload the file to your website. You can use the file manager provided by your hosting service or an FTP client like FileZilla to upload the file. Make sure you are uploading the .htaccess file to the same folder where WordPress is installed.
How to Edit the .htaccess File
There are several ways to edit the .htacces file of your WordPress site. Let me discuss the top three ways here –
Using a WordPress Plugin
You can use a WordPress plugin like the WP .Htaccess Editor to edit the .htaccess file. Once you install and activate the plugin, it will create a new menu item titled “Htaccess” in the dashboard.
Go to Htaccess > Htaccess Editor. This will take you to a page like the following where you can edit the .htaccess file –
Make your changes and click the “Save file” button to save the changes.
The plugin also keeps a backup of the original .htaccess file. You can restore the original file by going to Htaccess > Backup and clicking the “Restore backup” button.
Using the Yoast SEO Plugin
If you are using the Yoast SEO plugin, there is a direct option that allows you to edit the .htaccess file. First of all, go to SEO > Dashboard. Click on the “Features” tab. Find out the option titled “Advanced settings pages” and enable it. Click the “Save Changes” button at the bottom of the page.
Next, go to the SEO > Tools page. Click on the “File editor” link. This will take you to a page where you can edit the .htaccess file.
After making the necessary changes, click the “Save changes to .htaccess” button.
Using the File Manager
All hosting services offer a file manager tool to control the files on your server. If you are using Hostgator, log in to your portal, and click on the “Hosting” tab. In the resulting page, click on the “File Manager” section. This will open up the file manager for your account.
Find out the .htaccess file and select it. If you cannot find the file, click the settings button from the top-right corner and check the “Show Hidden Files” option.
Next, open the .htaccess file with the code editor. Make the necessary changes and click the “Save Changes” button.
If you are using other hosting services, you can follow the same procedure – open up the file manager, find the .htaccess file, and edit the file with the code editor.
.htaccess sounds like something uber-technical that should be managed by the nerds and techies only. Now that you have read this in-depth tutorial, you know that is not the case. With the right direction, anyone can create and edit this file.
So, what are you waiting for? Follow the steps provided above and create the .htaccess file for your WordPress site today. Let me know if you have any question about the process.