Understanding the .htacess file is necessary to learn WordPress. It can unlock the variety of feature WordPress has to offer.
You can rewrite URLs, password protect the directories itself, enable hotlink protection, and even change your website’s time zone. However, editing and playing with .htaccess file is not a game, as a single slip could break your site.
In this article, I will tell you about .htaccess. You will learn to find, create and edit .htaccess file while taking precautions.
What is the WordPress .htaccess file?
The .htaccess file is a file to configure command for the server. The file contains instructions to handle things on the website.
For example: how to redirect users, set a password for the files, lock the directories etc
These instructions must be written in a proper format so it could be read by the server.
.htaccess file is typically located in the WordPress site’s root folder by default. The .htaccess file is a powerful way to command servers, and hence dangerous.
Before editing .htaccess, you should take the backup of the WordPress site.
How to find the WordPress .htaccess file?
As I mentioned before, the .htaccess file is important to the WordPress core. This file is typically hidden, as WordPress protect the file from any accidents.
You can find the .htaccess file in the file manager under cPanel. All the popular and quality web hosting service providers provide cPanel.
Login to your hosting account, and open the cPanel. From the cPanel, launch the file manager.
In case the file is hidden, you can unhide from the setting. Click on the ‘Setting’ button at the top left. A box will pop-up. Tick on the ‘Show Hidden Files’ and press Save.
Now the .htaccess file will be visible in the public_html folder.
Here it is. You successfully found the .htaccess file in the cPanel >> File Manager.
How to create a .htaccess file?
In rare cases, you will not find the htaccess file. It means WordPress has not made any during installation.
You can create .htaccess files simply creating the permalinks.
For that, close everything without saving, and login into your WordPress admin dashboard.
Navigate to the Permalink page from the sidebar. Setting >> Permalink.
Scroll down and click on ‘Save Changes’ button, without actually changing anything. This will instruct WordPress to create the .htaccess file.
Go back to File Manager via cPanel, and find your .htaccess file.
If it is still not there and WordPress failed to generate a .htaccess file, you have to create one manually. Creating a .htaccess file is easy, and you can do it from cPanel.
Steps to create a .htaccess file in the cPanel:
Step 1: Click to ‘+ File’ on the top bar of the File Manager
Step 2: Name the file .htaccess in the public_html and save it
Step 3: Edit the file to add the .htaccess code
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Step 4: Save the File
That’s it, you have created the .htaccess file.
How to edit the .htaccess file?
There are two easiest ways to edit the .htaccess file.
- Editing .htaccess file via cPanel
- Editing .htaccess file via Plugins
Editing .htaccess file via cPanel
Steps to edit .htaccess file via cPanel:
- Login to your cPanel
- Navigate to the root folder public_html
- Locate the .htaccess file
- Use the right-click to open the box
- Click the edit button
- Add the code you need to add
- Save the file
Editing .htacces file via WordPress plugin
If you do not want to dive into cPanel, you can use a WordPress plugin.
Install and activate Htaccess File Editor plugin from the WordPress directory.
After activating, you will find the plugin page in the setting. Navigate to the page and open the editor.
Add whatever the code snippet you need to add. Save changes and it’s done.
Take Away
Understanding .htaccess is the first step to learn WordPress at an expert level. Knowing how to edit .htaccess file comes handy in lots of situation.
In this quick tutorial, I showed you how to finds, create and edit .htaccess file in WordPress.
I hope this article helped you. If there is any question or query, please leave it in the comment box.