Magento 2 .htaccess file

Htaccess is a simple plain text file, that performs a number of important functions. Being placed on the server, htaccess file helps to control user’s interactions with the site and prevent some traffic from reaching your website. The file has no name, only the .htaccess allows the server to identify and use it.

Note, that the htaccess file is usually hidden and couldn’t be seen with a file manager. You can change it by activating the «Show Hidden Files» option.

Htaccess: pros and cons

Usually the htaccess file is used in situations, when you are not allowed to get access to the main server configuration file. For example, when use a web-hosting, the access to the configuration file may be denied. So, the htaccess file becomes a solution. But nowadays it is strongly recommended to avoid using the htaccess if possible. There are two reasons for that:

1. Htaccess slows down the performance and page loading speed, because httpd is looking for the file in every directory.
2. You can face difficulties if you permit users to change server configurations. You won’t be able to control some of these
changes. On top of that, if you give users less privileges, it may lead to more support requests.

Htaccess usage scenarios

Now, let’s switch to concrete situations, when the htaccess file can be useful, if you don’t have an access to the main server configuration file.

First, the file may be used to protect some areas of your website with a password.

Second, the htaccess file can do the following:

  • forbid hotlinks;
  • rewrite URLs;
  • set default pages;
  • create redirects;
  • reconfigure account settings and more.

Keep in mind to create a backup of your current htaccess file every time before making any changes to it. That simple rule can save you a lot of time.

Htaccess usage examples

Let’s consider two situations, when the htaccess file may be useful. First, you want to set a password for any area of your Magento 2 store using the htaccess file, add the following to it:

AuthType Basic
AuthName "Password Required"
AuthUserFile "/www/passwords/password.file"
AuthGroupFile "/www/passwords/group.file"
Require group admins

Second, you can enable Server Side Includes for a certain directory. Add the following code to the htaccess file in the target directory:

Options +Includes
AddType text/html shtml
AddHandler server-parsed shtml

There were two simple examples of the htaccess file usage.

SEO Suite Ultimate

The first Magento 2 SEO solution. Eliminates duplicate content issues, improves website indexation and makes it search engine & user friendly.


Post Comments

You must be logged in to post a comment. Click here to log in.

When a new tab opens, log in or create an account. Reload this page when logged in.