Htaccess (encoding): setting, examples of use
Everyone, surfing the Internet,found himself on such web pages and sites that are incorrectly displayed. For example, by opening a site, the user sees a variety of scribbles instead of a clear text, among which you can distinguish hieroglyphs, arcs, symbols and other signs. The most likely to see similar when you go to any Chinese or Japanese site. All this is due to the fact that there are used other settings in the .htaccess file. The encoding in those countries is different. A properly configured file will make it possible to avoid the appearance of such incomprehensible symbols on the site.
What is the encoding for?
For the site there is a basic encoding thatcalled Default Charset. It is included in a special .htaccess file. The encoding is necessary in order to correctly determine the rule according to which the characters will be converted to code values.
A couple of decades ago, the encodings were quitesmall. They included no more than seven bits of information. An example can be ASCII encoding. Now they have a very large number. Among them - and the most popular utf-8, Windows-1251.
The bottom line is that documents that havedifferent encodings, will look different. It is for this reason that errors appear on the pages of the site. The user's browser can be configured for one encoding type, and the .htaccess file on the server contains another. As a result, all the text on the pages turns into a set of incomprehensible symbols.
Basic Encodings
AT .htaccess you can specify the parameter by which the main encoding for the entire web portal will be determined. In addition, you can register other encodings, which will allow them to be displayed correctly on all pages of the site. Web developers in .htaccess encoding is usually specified in two types - utf-8 and Windows-1251. They are by default used by a large number of browsers and text editors.
To avoid the formation of scabs thatit is impossible to disassemble, it is necessary either to set the main encoding of the entire site using the parameter Default Charset, and also in .htaccess to forbid the conversion of files or to register all the necessary encodings. After all, the main problem is that, for example, the user can send some data in a perfect different encoding, which the server can not read. Such actions must be warned in advance.
How to set the site encoding in the .htaccess file
The default Charset encoding, which is key,processed by the server by default and sent to the browser, is specified using the AddDefaultCharset option. It is necessary to add the default encoding setting. When sending HTTP headers, a rule is assigned to them, which tells the browser what encoding all the characters on the site should be considered. That's why in the Content-Type section you need to specify the correct encoding. This is the setting for .htaccess.
The above option is essentialA directive for configuring the encoding of a site in a file. This command is able to finally solve the problem associated with scripts that by default use unknown encodings for Russian-language browsers, and not those that are needed. If you do not specify a parameter, the browser would throw an error. Otherwise, all documents are automatically recoded. It should be noted that not all browsers see encodings the same way. For this reason, it is better to use the most famous utf-8 and Windows-1251. They are known to all browsers.
From encoding problems you can get rid ofhelp activate automatic document conversion. For this, a special option CharsetSourceEnc is included in the .htaccess file. After it, you need to write the main character set. All the rest will be recoded into it. If, for one reason or another, you need to remove transcoding, you should write the CharserDisable On command in the file.
Specifying another encoding for individual sections of the site
This does not happen often, but sometimes there isThe need for one page or the whole part of the section to be displayed in a different language. Accordingly, we need another encoding. If you find the .htaccess file with the specified encoding in the root directory, all pages of the site will be displayed in it. The result may be that a person will go to a foreign section of the website, and there instead of the text in the language he needs, a group of strange symbols and letters will be displayed.
In order to understand this problem,need additional configuration. htaccess. It is necessary to create one more same file and add the AddType command in it, which specifies the additional encoding. This file can act both on the entire website, and on individual pages. This depends on where the .htaccess file is located. If you place it inside a folder with a specific site language, its configurations will only work on this part. The rest will function according to the rules of the first .htaccess, which is in the root directory of the site.
Redirect with .htaccess
Many website developers are faced with suchThe situation when, when changing an existing project, you need to keep the old addresses of some pages. Especially often this happens when the content management system on which the site is operating changes. It happens that initially the site was static and nobody cared about the logicality of addresses. Requires redirect (.htaccess requires changes).
Redirection is also needed if the administrator wants to maintain the position of the site pages in the search engines. After all, raising the rating from scratch is a difficult task.
What is needed to ensure that such aforwarding? .htaccess should be used on hosting. Redirect in it is carried out by means of option Redirect 301, after which the address of the new page is specified.
Access limitation
In order to restrict access to the entire site,its sections or certain files that are on the server, also uses .htaccess. Access is forbidden so that users who can spoil something do not access the files.
The following commands are used:
- To deny access to the entire site in the file, the command deny from all is prescribed.
- To deny access from a specific ip-address, use the deny from ip_address_user command.
File protection
Most likely, each user metabsolutely identical articles on different websites. Most often it happens that they are simply stealing from other resources. Of course, search engines are struggling with such a phenomenon as duplicating pages, but very often they do not quite rightly choose the source. For copywriters and programmers, this is a very serious problem. After all, in order to write quality content, it takes a lot of time and effort. And then the search engine imposes sanctions for the copied material. It is unpleasant. Often text is also stolen and images. In this case, a large number of such thieves do not even download images from foreign resources. In order not to overload their servers, they simply indicate links to pictures from the source. Such a link insertion is called a hotlink. In addition to the links to the pictures, you can insert them, pointing to the files that are available for download. For the author of content, this phenomenon is extremely unpleasant. The server of the source is also overloaded because of them.
In file .htaccess you can specify files that will be protected from hotlinks. It specifies extensions to which access will be denied. Also for the hotel files or entire groups, you can set a password. Then the .htaccess file is placed in the directory to which you want to close.
Error Diagnosis
It happens that there is an error with the code "500"after editing or placing the .htaccess file. This error occurs because of the shortcomings in the file itself. You can find out the reasons in the file that is in the domain directory, in the folder with the reports.
Also, if you do not do what is specified in .htaccess, you need to make sure that the processing of this file is enabled in the site settings.
As you can see, the .htaccess file is necessary both for setting up the encoding of websites, and for other useful operations that allow you to competently manage the development and support of various projects.