Wednesday, June 30, 2021

Understanding and Configuring the .htaccess File in WordPress

Understanding and Configuring the .htaccess File in WordPress

If you are a website owner, you probably already know a little bit about .htaccess files or at least know about their existence. In this tutorial, we will help you understand the purpose of .htaccess files. We will also discuss how you can access and configure the .htaccess file for your WordPress website.

What Does the .htaccess File Do?

The .htaccess file is used by Apache web server to determine how it should serve files to visitors. You can use this file to provide different configuration options for different directories. The options set by you in an .htaccess file inside a particular directory will apply to all the files and sub-directories within that directory.

You can use an .htaccess file to do a lot of things like rewriting URLs, redirecting users from a non-secure to secure version of your webpages, or controlling caching.

Where is my WordPress .htaccess File Located?

Usually names of files contain the actual base name and an extension like contacts.pdf and log.txt. However, the .htaccess file doesn't have an extension. It starts with a dot because this makes it hidden on the Unix and Linux operating systems. The file is located in the root directory of your WordPress installation.

However, you might not see it at first because your FTP client might be hiding it. If you don't see the .htaccess file in your WordPress root folder, look for an option in your FTP client to show hidden files.

htaccess file inside FTP clientshtaccess file inside FTP clientshtaccess file inside FTP clients

After that, you should see the file like the image above. The file name will probably still be lighter in color compared to regular files. If you still don't see an .htaccess file, see below for some troubleshooting tips.

How Does WordPress use .htaccess Files?

The core WordPress itself uses the .htaccess files to redirect URLs in order to make them readable as well as SEO and user-friendly. The file is located in the root directory of your WordPress website. This is the location where you first installed WordPress. It will also contain another file called index.php in the same directory as the .htaccess file.

If you log into the WordPress admin dashboard and then go to Settings > Permalinks, you will see that WordPress allows you to pick a format for creating the URLs for any posts you publish. The URL can contain a mix of month, day, post category, and post name among other things.

Choosing one of these settings and clicking on the Save Changes button will prompt WordPress to update the .htaccess file with some URL rewriting conditions. Your .htaccess file will then have some URL rewriting rules that look similar to the ones listed below.

Your own .htaccess file could contain many more rules depending on the plugins that you have installed on your website. For example, any security plugins that you have installed can add rules to the file in order to prevent access from certain IP addresses or allow access only from whitelisted IP addresses. Security plugins can do many more things inside the .htaccess file like prevent spam bot comment and hotlinking of images.

The following image shows some rules written inside the .htaccess file by the All In One WP Security plugin.

All in One WP Secutiry PluginAll in One WP Secutiry PluginAll in One WP Secutiry Plugin

You will also find some additional rules related to caching different types of files. These might be set if you have installed any WordPress optimization plugins that handle caching. Here is an image with some rules about expiration dates of caches of different types of files. These were added by a caching plugin.

Caching Directives by Caching PluginCaching Directives by Caching PluginCaching Directives by Caching Plugin

Problems in Finding and Updating .htaccess Files

The .htaccess file is supposed to work with Apache webserver. This means that you won't find a working .htaccess file on other servers like Nginx. If you have looked around in your root directory and still don't see an .htaccess file, please confirm from your web host if you are running on Apache or some other technology.

WordPress also needs to have proper permissions in order to read and modify an .htaccess file. You will need to change file permissions to give WordPress the ability to update the file if it is not working by default.

Understanding the Basics of .htaccess Files

Any security and caching plugins that you install on your website will usually do more than simply writing a bunch of lines inside your .htaccess file. However, having a basic understanding of some common commands will make you more comfortable when it comes to modifying an .htaccess file if the need arises. This can also help you avoid installing any plugins to do simple tasks like redirecting users to secure pages or from an old non-existing URL to a new one.

It is always important to take a backup of your original .htaccess file before you start making any changes to it. This is because any mistake while changing the file can take your website down or make it behave unexpectedly. Having a copy of the original will allow you to get the website up and running by simply replacing the changed file with the original version.

You can write your rules as well as comments to provide instructions to others or explain why you wrote those rules. Any comments in an .htaccess file must begin # as the first character. We will show you how to complete some basic tasks by directly editing your .htaccess file instead of installing a plugin.

Redirecting Old URLs

Let's say you had a popular post on some old URL that no longer exists. This could be something related like an older version of a product that you no longer sell etc. In such cases, you might want to redirect people visiting the old URL to your new webpage.

You can add the following line to you .htaccess file to do so. Remember to replace the paths with your actual URL values.

You can also redirect your entire website to a new URL by using the same command. In my case, the WordPress installation is in a directory named wordpress. So, I can add the following line inside my .htaccess file to redirect it somewhere else.

After you add the above line, someone visiting /wordpress/some-post/ will be redirected to https://ift.tt/3qCun1m.

Caching Common File Types

Regular visitors to your website will request the same CSS, JavaScript and image files again and again. Therefore, it makes sense to let them get those files from cache instead of a regular transfer. You can add basic caching functionality with an .htaccess file. Here is an example that caches these files.

The number after A sets the total number of seconds for which the files will be cached. There is another way of setting a caching period by specifying the time period as human readable values.

You should now be able to specify caching instructions for file types that are not covered by caching plugins.

Redirecting Users to Secure Pages

Another use of .htaccess file is to redirect users from HTTP pages to corresponding HTTPS pages. You can use the following rules to do so.

The flags R and L can be in any order. The R flag tells the server to send a specified response code and the L flag tells the server to stop processing of any more rules in the set.

Final Thoughts

We began this post with a brief explanation of the purpose of .htaccess files. After that we learned where you can find these files in WordPress and how WordPress and other plugins use it to do things like caching resources and improve security. Finally, we discussed some common rules and command that you will find in your own .htaccess files and how to add them if they don't already exist.

There is a lot more to learn about .htaccess files. One way to get started is to read this general purpose introductory .htaccess tutorial from Apache.


No comments:

Post a Comment