WordPress .htaccess File Explained for Non-technical Users

Last updated - July 8, 2021

The WordPress .htaccess file is a core file in your WordPress folder. It is defined as a “distributed configuration file” that helps Apache web server to handle changes in configuration for each directory. If you are a WordPress website owner, you may have heard of .htaccess file mentioned often in tutorials. In this article, we will try to explain WordPress .htaccess file and its importance for non-technical users.

The importance of WordPress .htaccess file

As mentioned above, the .htaccess file has instructions for Apache web server. The primary role of the .htaccess file is to control the display of your site’s permalinks. That means when you make changes to your site’s permalink structure, the .htaccess file will be updated. In certain scenarios, .htaccess file is not created until you customize the permalink structure.

Though the primary function of .htaccess file is associated with the permalink display, there are certain other aspects where this file is relevant. These are:

  • To set up 301 redirects on your site.
  • Ensure https is used instead of http.
  • To display custom error pages.
  • Tighten security of your site by restricting access.
  • To modify the maximum upload limit.
  • Caching.

How to access the WordPress .htaccess file?

You will be able to access your WordPress site’s .htaccess file using your hosting service’s control panel or by using an FTP client like FileZilla. Also, you need to be extremely cautious while making changes to this file, as it can potentially break your site if modified incorrectly. You will find the .htaccess file in your domain’s root directory.

WordPress .htaccess File
This is an example of .htaccess file.

You can open the .htaccess file using any text editor, make necessary modifications, and save it. Once this is uploaded to the server, the changes will be in effect.

What if .htaccess file does not exist?

As mentioned earlier, sometimes .htaccess file will not be created until you save the permalink structure of your site. If this is the case, all you have to do is visit the Permalink structure settings (Settings > Permalinks) on your site, and Save the settings once. You don’t even need to make any modifications here.

Different uses of WordPress .htaccess file

Here, we will discuss some of the common uses of WordPress .htaccess file and how you can handle them.

For url redirects

You will be able to manage 301 redirects using the .htaccess file. To help you understand better, we will mention a simple redirect option. According to your requirement and knowledge level, you can attempt different options.

To redirect an old page on your site to a new page, you can add the below code in your .htaccess file (please modify the details accordingly).
Redirect 301 /oldpage.html http://www.yourwebsite.com/newpage.html

Forcing https

To make your site safer, you can use an SSL connection, and redirect traffic to https rather than http. For this, you can try the below code in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://mywebsite.com/$1 [R=301,L]

Password protection

You can use the .htaccess file to create password protection on your website. For this, create .htpasswd file and upload it to the root directory. This file will have information on the username and the password. Once you have uploaded the .htpasswd file, you need to modify the .htaccess file to instruct the server about the password protection.

For example, you can add the below code to apply password protection for a specific file on your site.

AuthName "Prompt"
AuthType Basic
AuthUserFile /wp-admin
Require valid-user

To increase maximum file upload size

Sometimes while uploading bigger files to your site, you may encounter difficulties if there is a limit based on file size.

You can use the .htaccess file to increase this limit.

According to this WPBeginner article, you can use the  following code to the .htaccess folder to manage this.

php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300

Htaccess File Editor Plugin

With the help of this plugin, you can easily edit .htaccess files on your WordPress site. Also, it allows you to test the file so that you can avoid making any syntax errors. Moreover, the plugin creates a backup .htaccess file every time you modify an htaccess file. This will help you easily restore the site in case of any issues.

WordPress .htaccess File
With this plugin, you can easily edit .htaccess file right from your WordPress admin panel.

Why editing .htaccess files is not always recommended?

As you probably know, if you are editing the .htaccess files without proper technical knowledge, it could break your site. In addition, there are certain other aspects pointed out by experts to avoid using .htaccess file. Here are some of those reasons.

  • It could affect site performance – When you are allowing .htaccess files, for all requests, Apache will look in each directory for .htaccess file. This could affect your page loading speed.
  • There will be security risks – Generally, modification of .htaccess file is done when content providers who don’t have access to the root directory need to make changes in configuration. That means you are allowing users to make changes in server configuration, which could lead to undesirable outcomes.

Best practices to follow while editing the .htaccess file

There will be certain unavoidable scenarios where you have to edit the .htaccess file. In such cases, you can follow these best practices:

  • Create a backup – As a general rule, before you make any code-level changes on your WordPress site, create a backup. This will help you restore the site back to its functional form in case the new code creates any issues. Also, you can use a staging site to test changes before implementing them. In addition, you can download the .htaccess file to your local computer before making any changes to it.
  • Keep it short and simple – While making modifications to the .htaccess file, take care to keep it simple and not too long.
  • Do not use too many .htaccess files – Experts recommend not to use too many .htaccess files on your site. If possible, use only one .htaccess file. Another aspect to take care is to save the .htaccess file in the site root directory.

We hope this article has helped you understand the WordPress .htaccess file better. Feel free to leave us a comment if you have any queries.

Further reading

LEAVE A REPLY

Please enter your comment!
Please enter your name here