Coastal Media Brand

There’s a lot you can do within WordPress to configure settings or troubleshoot issues with your website. However, sometimes you need to go into the backend and edit root files like .htaccess to complete certain tasks.

In this post, we’re going to take a closer look at the WordPress .htaccess file — what it is, where you’ll find it, and how to use it to secure, optimize, and troubleshoot errors on your site, amongst other things. 

What is the WordPress .htaccess File?

.htaccess is an Apache-based configuration file that gives WordPress users the ability to do things like:

The .htaccess file is also helpful in troubleshooting WordPress errors. For instance, resetting the .htaccess to the default can fix the 500 internal server error as well as the 403 forbidden error if nothing else has worked. 

If your WordPress website is hosted on an Nginx web server, you won’t have an .htaccess file. However, you should be able to do these types of configurations at the server level in other ways.

Where Is the .htaccess File in WordPress?

You won’t find the .htaccess file inside of WordPress out of the box. To locate the file, you’ll need to go into your server files. You can access them using FTP or a File Manager in your cPanel. 

For the rest of this post, I’ll use SiteGround web hosting to demonstrate how to find and use the .htaccess file. If your website is hosted elsewhere, the location of your website tools might differ, but the remainder of the guidance in this post will be relevant.

To locate the .htaccess file, open your FTP or File Manager. You’ll find both under Site Tools and Site.

In SiteGround's control panel, users can access their root folder under Site and FTP or File ManagerIn SiteGround's control panel, users can access their root folder under Site and FTP or File Manager

Select the one you want to use. Then, go to the root of your website, which is a folder labeled public_html

SiteGround users will find the .htaccess file in the root (public_html) of the serverSiteGround users will find the .htaccess file in the root (public_html) of the serverSiteGround users will find the .htaccess file in the root (public_html) of the server

If you do not see the file there, check to make sure you’re using an Apache web server. If your web hosting uses Nginx instead, that’s why you aren’t seeing the file.

If you do have an Apache server, then the file might be hidden. The leading dot in .htaccess usually means the file will be hidden from view, but that’s not always the case depending on your cPanel’s default settings.

Look for a settings or preferences menu in your FTP or File Manager. You should find a setting that enables you to show hidden files. It also may appear as an optional checkbox when opening your server files for the first time.

How Do You Edit the .htaccess File?

You can manually edit the .htaccess file from your FTP or File Manager. Another option is to edit it using a WordPress plugin

Before making any changes to the file, back up your website. You can make a backup from within your web hosting account or use a WordPress backup plugin.

Manually Editing the .htaccess File

There are three ways to open the .htaccess file from your FTP or File Manager:

  • Right-click on the file name and click Edit
  • Select the file and click the pencil icon in the toolbar
  • Double-click on the file
Click the pencil icon to edit the .htaccess fileClick the pencil icon to edit the .htaccess fileClick the pencil icon to edit the .htaccess file

You can make changes to this file just as you would in a text or code editor.

An example of what the .htaccess file looks like in the SiteGround File Manager editorAn example of what the .htaccess file looks like in the SiteGround File Manager editorAn example of what the .htaccess file looks like in the SiteGround File Manager editor

Here are some examples of commands you might want to add to the file: 

To blacklist an IP address:

1
order allow,deny
2

3
deny from 76.106.149.140
4

5
allow from

To implement a 301 redirect: 

1
Redirect 301 /old-url 

To enforce HTTPS:

1
RewriteEngine On
2

3
RewriteCond %{HTTPS} off
4

5
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Editing the .htaccess File with a Plugin

To edit the .htaccess file with a plugin, install and activate Htaccess File Editor inside your WordPress site. 

You’ll find the settings panel for the plugin under the Settings > WP Htaccess Editor. Read through and agree to the disclaimer that explains what you need to do if your website goes down when editing the file.

This will then unlock a version of the .htaccess file editor inside of WordPress.

The WP Htaccess Editor plugin brings the .htaccess file editor into WordPressThe WP Htaccess Editor plugin brings the .htaccess file editor into WordPressThe WP Htaccess Editor plugin brings the .htaccess file editor into WordPress

Make your edits to the text. Click Test Before Saving to ensure your changes do what you want them to do and don’t break your site. 

If you’re satisfied with the edits, click Save Changes.

How Do I Create a New .htaccess File for My WordPress Site?

There are two ways to create a new WordPress .htaccess file: 

Option 1: Manually Create a New File 

There are a couple of reasons why people need new .htaccess files. Either they can’t find the file on the server to modify or they want to add one to a subdirectory on the server. While it’s true that the .htaccess file typically lives in the root folder, you can also add one to other areas in order to set directory-level rules and permissions. 

To create a new file, you’ll need to open the FTP or File Manager. You then have two ways to manually add it. 

The first way to do it is to add a New File. In SiteGround, the icon is a page with a small plus-sign icon in the bottom-right corner.

In the Siteground File Manager, users can click New File to create a blank fileIn the Siteground File Manager, users can click New File to create a blank fileIn the Siteground File Manager, users can click New File to create a blank file

Click the icon and a new file will open for editing. 

Paste the following 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 want to make customizations or add anything else to the file, do so now. Otherwise, save your changes and exit out of the editor. 

The other way to add a new file is by uploading one. In SiteGround, the icon is a blank page with an arrow pointing upwards.

In the SiteGround File Manager, users can upload a premade file to the rootIn the SiteGround File Manager, users can upload a premade file to the rootIn the SiteGround File Manager, users can upload a premade file to the root

You can create an .htaccess file using your computer’s text editor or word processor. Then save the file with the exact name .htaccess (no other extensions). 

Go to the root of your site. Then click the File Upload icon to insert it.

The other reason you might need a new .htaccess file is if your website is experiencing an error. If the error exists in that file, then replacing it with a clean one will repair the error. 

To do this, log into WordPress. Then go to Settings > Permalinks.

The WordPress Permalinks settings page provides a quick way to create a new .htaccess fileThe WordPress Permalinks settings page provides a quick way to create a new .htaccess fileThe WordPress Permalinks settings page provides a quick way to create a new .htaccess file

Select any of the other permalink structures. Then click Save Changes at the bottom of the page. 

Anytime the permalinks change in WordPress, the .htaccess resets. This will clear out any customizations made by you, a plugin, or a hacker, and reset the file to its default state. 

If you don’t find an .htaccess file on your server, this is another method you can use to instantly generate one. Just remember to change your permalink structure back to the preferred format after the file has been generated.

Summary

The .htaccess file might not get a lot of use, but it can help you make critical changes to your website in a matter of seconds. Whether you want to strengthen security, improve performance, troubleshoot an error, or something else, most of these things can be achieved by adding a few new lines of code to the WordPress .htaccess file. Knowing what it does, where to find it, and how to responsibly use it will ensure you make the most of its capabilities.

Coastal Media Brand

© 2024 Coastal Media Brand. All rights Reserved.