One of the useful and important tools for configuring a physical or VPS server is a special configuration file with no extension, named .htaccess. If your site is hosted and runs on a web server powered by Apache software, you definitely need to know the capabilities of this important management tool that is available to you.
What the .htaccess file is and what it is responsible for
Unlike the main configuration file of any Linux server running Apache - (httpd.conf), the tool we are interested in is responsible for the local settings of the directory or subdirectory it is placed in. That is, if you put this file in the server root, the rules it sets will apply to all the contents, except for directories that have their own .htaccess overriding the local settings.
Below are the main capabilities .htaccess provides when configuring a website:
- Automatic redirection to the desired page;
- Access control for directories and subdirectories;
- Defining rules for building human-readable URLs;
- Requiring authorization for access to directories;
- Creating custom error messages;
- Controlling access to content by IP address;
- Restricting the activity of search engine crawlers;
- Overriding the existing page encoding.
And this is far from a complete list of the useful settings this file can handle.
Where to find and how to create .htaccess files
While the main configuration tool httpd.conf is available only to hosting administrators, the additional .htaccess configuration file can be used by the site owner on their own to optimize interaction with the Apache software.
As mentioned above, our additional service file can be located in the site's root directory or in any other directory that needs additional settings.
You can create a .htaccess file in standard text editors such as «Notepad» or «Notepad++». To do this, follow a few simple steps:
- Launch your chosen editor.
- Select the «Save As» option.
- In the list of extensions, choose «All Files».
- For the name, type only the extension .htaccess.
- Save your new service file.
Now you can set the necessary configuration rules by placing the .htaccess file in the appropriate directory on the remote server.
The encoding .htaccess should use
By default, the browser does not always pick the site's encoding correctly. This leads to the resource's content being displayed incorrectly. To avoid this common mistake, when creating the service file or working with existing .htaccess tools, specify the correct encoding in them. It must strictly match the encoding set for the site itself, for example, UTF-8.
The necessary rules for the resource's directories and subdirectories are set when editing the additional service file using a special set of directives, which are not covered in this article due to their sheer number.