File .htaccess: what it is, why it is needed, how to create and configure correctly

The .htaccess file is a specialized service document used to change the settings of the Apache web server and similar servers. Simply put, its use will help you to change the work of the site: configure access to folders, files and other materials, specify the rules of census links and warning messages about errors. At the same time, you do not change the functioning of the entire server, adjusting only additional parameters for some users.

When the site owner is working on his project, the moment will come when he will need to use the .htaccess file. Despite its great importance, for many webmasters, especially newbies, it remains unexplored, because to understand it you need to know well not only SEO, but also programming.

In this article we will help you understand why you need .htaccess and how to configure it. This is very important because it opens up flexible possibilities for site owners who want to ensure reliable security for their resource.

Opportunities and what is needed

In this file, site builders can:

  • Configure simple redirection directives. This will allow, after changing the domain, to send a visitor from the page of the previous site to a new project.
  • Redirect from the URL where the www is to the domain without www . Or on https after installing the ssl certificate.
  • Describe errors. Among them is the error 404 with the replacement of the necessary page.
  • File caching It is done to accelerate the work of the web resource.
  • Create CNC. These are “Human-understandable URLs”, that is, links that are understandable for Internet users are formed.
  • Change the source code of pages.
  • Manage access to files and directories with passwords.
  • Set index file.
  • Open and close access from specific IP addresses.
  • Manage search bots on the site.
  • Configure directives in a complex direction.

The main purpose of .htaccess is to customize the site and directories in accordance with certain requirements. That is, through this file, SEO specialists and programmers can change the settings of the web server, even without administrator rights. But changes are made only for a specific site, and they do not affect the server itself.

Server configurations are changed only with the use of directives (commands) that include a “key” and a “value” for it. All the most important directives that allow you to control the server are in the main configuration file called httpd.conf. The problem is that the average user does not have access to it, because there is a large number of parameters that affect the performance of the entire server. That is why .htaccess is relevant, allowing you to change some directives in the main file.

All adjustments in the auxiliary file affect the entire directory in which it is located. And if you download this document to the root folder of the site, the changes will affect the entire resource.

Where is .htaccess

You can easily check if you have a service file. It differs from many other documents in that it has only an extension in the form of a name from a word, whereas there is no name itself. Yes, we are all used to seeing files with the name of a word, and an extension after the dot, which consists of only 2-3 letters. But empty names allow you to make files and folders “hidden”, open source. From this it follows that .htaccess for ordinary users remains invisible, so they can not change it.

But this method of “hiding” a file has a reverse side of the coin. In standard FTP clients on Windows and Mac, the document is very often invisible to users, which is why they mistakenly believe that it simply does not exist. Therefore, a person creates a new file to make their configurations, codes for error handling, creating access to folders, etc. It’s good that most hosting companies leave space on the disk where the htaccess file is already installed automatically.

Location htaccess on the server

But if you do not find it, then try to go to the file manager, which is built into your hosting. It can show hidden files and directories. Look for it in public_html, and then use the text editor installed in the program to edit and save the file, after which it will be uploaded to the server yourself.

If you do not find the document in the specified path, then create your own. It may happen that an error appears on the screen. So, this hoster has forbidden to create a service file.

Inexpensive hosting sites often prohibit customers from creating and modifying such a document. Thus, they encourage the use of their services at more expensive rates, where it is allowed.

How to create .htaccess

To do this, you need the most common text editor, for example, Notepad, which is on any PC:

  1. Open Notepad, do not write anything in it.
  2. Select “File” and click “Save As . 
  3. A window will appear where, next to the “File Type” item , select “ All types” or “All files” .
  4. In the line “File Name” enter htacces and click on “Save” .

Next, double-check whether the document is created correctly. Be sure to see if you saved it as .htaccess.txt. Then upload the file to the root folder of the server, and if the error 500 does not appear, it means that you did everything correctly.

From now on, you can edit the file, allowing you to manage your website more flexibly.

Examples of using

Consider the most popular commands for .htaccess.


These directives are used with enviable regularity. They allow you to redirect the visitor from the old URL to a new page. This is possible thanks to a 301 redirect. Enough to enter in the file code:

Redirect 301 / old_URL.html http: // of

In general, the directive will be displayed in this form:

Redirect [status] URL_LOCAL URL_REDIRECT

URL_ LOCAL is the old address from which the user is being transferred.

URL_ REDIRECT – the new URL where the page is transferred.

In the status] field can be the following values:

  1. 301 – page moved forever.
  2. 302 – page moved to time.
  3. 303 – see another page.
  4. 410 – page removed.

Mod_rewrite (complex carry directives)

This module is contained in Apache. It has an abundance of a variety of directives for advanced address management. The main ones include:

1. Specifying the primary mirror

Designed to convert a domain from www to a URL without www. Just enter the rule:

RewriteEngine On # includes work
RewriteCond% {HTTP_HOST} ^ www.your_site_name \ en $ [NC] # conditions for starting redirection
RewriteRule ^ (*) $ http: // your_site_name _ / $ 1 [R = 301, L] # transformation rule

2. Transfer to https

Today, Google search engine actively encourages site owners to use a secure connection, for which it is necessary to redirect users from http to https using code:

RewriteEngine On
RewriteCond% (HTTPS)! On
RewriteRule (. *) Https: //% (HTTP_HOST)% (REQUEST_URL)

3. Putting a slash at the end of the address

If you do not want the URL of the page to end with the directory name http: // , enter the code in the file:

RewriteCond% {REQUEST_URI} /+[^\\++
RewriteRule ^ (. + [^ /]) $% {REQUEST_URI} / [R = 301, L]

After executing this command, in the future, after the address in the automatic mode, a slash will be added: http: // name of

4. Domain transfer

When changing the site name, do not forget to specify in .htaccess:

RewriteEngine on
RewriteCond% {HTTP_HOST} ^ www \ \ .ru $ [NC]
RewriteRule ^ (. *) $ Http://$1 [R = 301, L]

By optimizing the site for both Yandex and Google, a change in the domain name can be fraught with unpleasant consequences. The instructions in robots.txt for Yandex overlap with a 301 redirect. But this can be prevented by adding a rule to the code:

RewriteEngine on
RewriteCond% {REQUEST_FILENAME} robots.txt $ [NC]
RewriteRule ^ ([^ /] +) $ 1 [L]
RewriteCond% {HTTP_HOST} ^ mysite \ .ru
RewriteRule ^ (. *) $ Http://$1 [R = 301, L]

5. Forbidden commands for the search bot

In .htaccess, as well as in robots.txt, you can deny access to the site for search engine robots:

RewriteEngine on
RewriteCond% {USER_AGENT} Googlebot
RewriteRule. * - [F] # F - gives error 403 - it is forbidden to scan

Error processing

Almost any Internet user encountered an error 404 not found (page not found or deleted). But there are a huge number of other mistakes that you probably have not heard about.

In the service file, you can register the file to display it during an error. But first you need to create the same file in the html extension, and register in it what this error is. Then, depending on the error used, add to the .htaccess encoding:

ErrorDocument 404 /siteerror404.html

Index Documents

When a visitor enters the site, the index.xml index file immediately opens. You can replace the index page with any other one by adding the code:

DirectoryIndex name.xml

Or add a few such pages, and the search engine will find them in the appropriate sequence:

DirectoryIndex index.html index.php

Specify Encoding

This function is needed to display the text on the user’s screen correctly. Otherwise, instead of a normal readable page, it will see incomprehensible characters. Today the encoding is widespread: Windows-1251 – Cyrillic and UTF-8 – two-byte encoding . To select the encoding in the service document, the following directive is applied:

AddDefaultCharset WINDOWS-1251

And so that later, when you upload files to the server, there are no problems, indicate that all new files will be converted to the same encoding:

CharsetSourceEnc WINDOWS-1251

Specifying passwords for directories

To do this, in the closing directory write:

AuthName "Need password" # message for password request
AuthType Basic # authentication type
AuthUserFile / passwords/.psd # file name containing login passwords
Require valid-user # name of users who have access

Access to files and directories

Through the .htaccess file, you can deny all or part of access to certain files. Suppose you want to close access to the system directory. Then in the appropriate .htaccess list:

Deny from all

To prevent viewing a specific file, for example, test.php:

<Files test.php>
deny from all
</ Files>

Open view for a specific IP, and at the same time close the rest, allows the command:

<Files "passwd.html">

Order Deny, Allow
Deny from all
Allow from 12.345.678.90
</ Files>

12.345.678.90 is an example of an IP address (here you need to specify the IP you need).


As you can see, the service file .htacces is one of the most important tools for the full performance of a web resource. And having familiarized yourself with its capabilities, you probably wondered why you knew little about this file before. This is an effective tool for SEO specialists and programmers to make changes to the web server configuration for certain sites without affecting the server itself.

Thanks to it, the owners of Internet projects can inform search engines and visitors about the domain change and transfer them to a new one, speed up site indexing , close access to all or specific resource data, prevent their updating and downloading by unauthorized persons, provide access to certain files by IP and password.

One Reply to “File .htaccess: what it is, why it is needed, how to create and configure correctly

Leave a Reply

Your email address will not be published. Required fields are marked *