Skip to content

How To Edit the .htaccess File

  • 4 min read
  • by

Introduction

Seeing the title of this article, you may be asking yourself, “Just what is the .htaccess file?” and also “Why should we edit the .htaccess file?” The answer to the first question is that the .htaccess file is a configuration file found (or created) in the root directory of your WordPress site. What it does, to answer the second question, is that it directs and changes the behavior of the server that contains your WordPress site. You may need to do this, to specify page redirections or even password protect your website among other things.

Creating/Editing the .htaccess file

The .htaccess file is created when we modify the Permalink Settings in the admin dashboard as can be seen in the GIF below.

Permalink Settings

Doing this will create an .htaccess file in your site’s root directory. (If you’re using UNIX/Linux you will have to list all files in the directory – including hidden ones).

HTACCESS File

If it wasn’t created, no need to worry. You can just create a blank file using a text editor and name it as a .htaccess file, then save it in the site’s root directory.

In case your site is remotely deployed, you may need to do transfer the .htaccess file through FTP if it isn’t created when your site’s permalinks configurations change.

Using multiple .htaccess files

You may have more than one .htaccess file in your WordPress site. An .htaccess file configures the settings of your WordPress site’s directories. The .htaccess configuration on the root will apply for all subdirectories in the site. Placing an .htaccess file in a directory will give a custom configuration for that directory. Subdirectory configurations override the configurations of a parent directory’s .htaccess.

So if you need more control for each directory in your site, multiple .htaccesss files is the way to go.

Uses of the .htaccess file

Now that we’ve seen how to create the .htaccess file let’s look at some examples of what it is used for.

Password Protecting Pages

The .htaccess file was originally used for password protecting pages. This is usually done also with an additional file – .htpasswd, which contains a username and a password (usually encrypted). .htaccess refers to .htpasswd similat to the following:

AuthUserFile /path/to/.htpasswd
AuthName "Name of Secure Area"
AuthType Basic
# This will restrict who can access files in the directory
<Limit GET POST>
require user johnsmith
require user janedoe
</Limit>Code language: PHP (php)

URL Redirecting

The .htaccess files allow us to redirect pages. By specifying the redirect routes, the browser will redirect to another URL, when another specified URL content is accessed. This is really easy to do, we just have to place the following lines:

# We can also use Redirect 302
Redirect 301 /accessed-url.html http://redirected-domain/full-url.htmlCode language: PHP (php)

IP Blacklisting/Whitelisting

We can also use .htaccess to blacklist IP addresses by adding the following:

order allow,deny
deny from 111.22.3.4
deny from 789.56.4.
allow from allCode language: CSS (css)

The previous snippet will prevent the specified IP addresses from accessing the directory. (if this is in the root this will prevent the IP addresses from accessing the site).

Conversely, we can also whitelist IP addresses, which would prevent all from accessing the directory, except the specified addresses:

order deny,allow
deny from all
allow from 111.22.3.4
allow from 789.56.4.Code language: CSS (css)

Other Uses

These are only some of what .htaccess can do. We can also use it for rewrite rules, troubleshooting, or scripting, For more uses, visit this link.

Conclusion

That’s all you need to know to get started with .htaccess. It may seem daunting at first to configure the file, but with experience, you’ll get very comfortable with it.

That’s it for this article. Thank you for reading!

Kristin Eitel

Kristin Eitel