What Is the WordPress .htaccess File and What Can You Do With It?

There’s rather a lot you are able to do inside WordPress to configure settings or troubleshoot points together with your web site. Nevertheless, typically it is advisable to go into the backend and edit root information like .htaccess to finish sure duties.
On this submit, we’re going to take a better have a look at the WordPress .htaccess file — what it’s, the place you’ll discover it, and tips on how to use it to safe, optimize, and troubleshoot errors in your web site, amongst different issues.
What’s the WordPress .htaccess File?
.htaccess is an Apache-based configuration file that provides WordPress customers the power to do issues like:
The .htaccess file can be useful in troubleshooting WordPress errors. For example, resetting the .htaccess to the default can repair the five hundred inner server error in addition to the 403 forbidden error if nothing else has labored.
In case your WordPress web site is hosted on an Nginx internet server, you received’t have an .htaccess file. Nevertheless, you must have the ability to do all these configurations on the server stage in different methods.
The place Is the .htaccess File in WordPress?
You received’t discover the .htaccess file within WordPress out of the field. To find the file, you’ll want to enter your server information. You may entry them utilizing FTP or a File Supervisor in your cPanel.
For the remainder of this submit, I’ll use SiteGround webhosting to reveal tips on how to discover and use the .htaccess file. In case your web site is hosted elsewhere, the situation of your web site instruments may differ, however the the rest of the steering on this submit shall be related.
To find the .htaccess file, open your FTP or File Supervisor. You’ll discover each underneath Website Instruments and Website.
Choose the one you wish to use. Then, go to the foundation of your web site, which is a folder labeled public_html.
If you don’t see the file there, test to ensure you’re utilizing an Apache internet server. In case your webhosting makes use of Nginx as an alternative, that’s why you aren’t seeing the file.
For those who do have an Apache server, then the file could be hidden. The main dot in .htaccess often means the file shall be hidden from view, however that’s not at all times the case relying in your cPanel’s default settings.
Search for a settings or preferences menu in your FTP or File Supervisor. It is best to discover a setting that allows you to present hidden information. It additionally could seem as an elective checkbox when opening your server information for the primary time.
How Do You Edit the .htaccess File?
You may manually edit the .htaccess file out of your FTP or File Supervisor. An alternative choice is to edit it utilizing a WordPress plugin.
Earlier than making any modifications to the file, again up your web site. You may make a backup from inside your webhosting account or use a WordPress backup plugin.
Manually Enhancing the .htaccess File
There are 3 ways to open the .htaccess file out of your FTP or File Supervisor:
- Proper-click on the file identify and click on Edit
- Choose the file and click on the pencil icon within the toolbar
- Double-click on the file
You may make modifications to this file simply as you’d in a textual content or code editor.
Listed below are some examples of instructions you may wish to add to the file:
To blacklist an IP handle:
1 | order permit,deny |
2 | |
3 | deny from 76.106.149.140 |
4 | |
5 | permit from |
To implement a 301 redirect:
1 | Redirect 301 /old-url https://yourdomainname.com/new-url |
To implement HTTPS:
1 | RewriteEngine On |
2 | |
3 | RewriteCond %{HTTPS} off |
4 | |
5 | RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
Enhancing the .htaccess File with a Plugin
To edit the .htaccess file with a plugin, set up and activate Htaccess File Editor inside your WordPress web site.
You’ll discover the settings panel for the plugin underneath the Settings > WP Htaccess Editor. Learn via and conform to the disclaimer that explains what it is advisable to do in case your web site goes down when enhancing the file.
This can then unlock a model of the .htaccess file editor within WordPress.
Make your edits to the textual content. Click on Check Earlier than Saving to make sure your modifications do what you need them to do and don’t break your web site.
For those who’re glad with the edits, click on Save Adjustments.
How Do I Create a New .htaccess File for My WordPress Website?
There are two methods to create a brand new WordPress .htaccess file:
Possibility 1: Manually Create a New File
There are a few explanation why individuals want new .htaccess information. Both they will’t discover the file on the server to switch or they wish to add one to a subdirectory on the server. Whereas it’s true that the .htaccess file sometimes lives within the root folder, you may as well add one to different areas with a purpose to set directory-level guidelines and permissions.
To create a brand new file, you’ll have to open the FTP or File Supervisor. You then have two methods to manually add it.
The primary approach to do it’s so as to add a New File. In SiteGround, the icon is a web page with a small plus-sign icon within the bottom-right nook.
Click on the icon and a brand new file will open for enhancing.
Paste the next default .htaccess code into the file:
1 | # BEGIN WordPress |
2 | |
3 | |
4 | |
5 | RewriteEngine On |
6 | |
7 | RewriteBase / |
8 | |
9 | RewriteRule ^index.php$ – [L] |
10 | |
11 | RewriteCond %{REQUEST_FILENAME} !-f |
12 | |
13 | RewriteCond %{REQUEST_FILENAME} !-d |
14 | |
15 | RewriteRule . /index.php [L] |
16 | |
17 | |
18 | |
19 | # END WordPress |
If you wish to make customizations or add anything to the file, achieve this now. In any other case, save your modifications and exit out of the editor.
The opposite means so as to add a brand new file is by importing one. In SiteGround, the icon is a clean web page with an arrow pointing upwards.
You may create an .htaccess file utilizing your pc’s textual content editor or phrase processor. Then save the file with the precise identify .htaccess (no different extensions).
Go to the foundation of your web site. Then click on the File Add icon to insert it.
Possibility 2: Change Your Permalinks Construction
The opposite motive you may want a brand new .htaccess file is that if your web site is experiencing an error. If the error exists in that file, then changing it with a clear one will restore the error.
To do that, log into WordPress. Then go to Settings > Permalinks.
Choose any of the opposite permalink buildings. Then click on Save Adjustments on the backside of the web page.
Anytime the permalinks change in WordPress, the .htaccess resets. This can filter out any customizations made by you, a plugin, or a hacker, and reset the file to its default state.
For those who don’t discover an .htaccess file in your server, that is one other methodology you need to use to immediately generate one. Simply keep in mind to alter your permalink construction again to the popular format after the file has been generated.
Abstract
The .htaccess file may not get a variety of use, however it will possibly allow you to make important modifications to your web site in a matter of seconds. Whether or not you wish to strengthen safety, enhance efficiency, troubleshoot an error, or one thing else, most of this stuff could be achieved by including a number of new strains of code to the WordPress .htaccess file. Understanding what it does, the place to search out it, and tips on how to responsibly use it’s going to make sure you take advantage of its capabilities.