ComputersInformation Technology

Htaccess (encoding): setting, examples of use

Everyone, surfing the Internet, came across 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 moving to any Chinese or Japanese site. All this is due to the fact that there are other settings used 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 coding for?

There is a basic encoding for the site, which is called 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 symbols will be converted into code values.

A couple of decades ago, the encodings were very small. 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 have different 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 type of encoding, 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

In .htaccess, you can specify a parameter that will use to determine the main encoding for the entire web portal. 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.

In order to avoid the formation of scribbles that can not be disassembled, you must either set the main encoding of the entire site using the Default Charset parameter, as well as in .htaccess, to prohibit the conversion of files or to register all 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 the key, processed by the default server and sent to the browser, is indicated 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 which encoding should look at all the characters on the site. 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 the main necessary directive in order to configure the site encoding in the 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 the same encodings. For this reason it is better to use the most famous ones - UTF-8 and Windows-1251. They are known to all browsers.

From problems with the encoding can get rid of by activating the automatic transcoding of documents. To do 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 it becomes necessary that one page or the whole part of the section is displayed in another 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, you need an 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 site developers are faced with a situation where 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. Redirection is required (.htaccess requires changes).

Redirection is also needed if the administrator wants to keep the position of the site pages in the search engines. After all, raising the rating from scratch is a difficult task.

What is needed for such a redirection to be organized? .htaccess should be used on hosting. Redirect in it is carried out by means of option Redirect 301, after which the address of a 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, .htaccess is also used. Access is forbidden in order to prevent users from accessing files that might spoil something.

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 has found absolutely 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 correctly 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 system 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 upload images from foreign resources. In order not to overload their servers, they simply point to images from the source. Such a link insertion is called a hotlink. In addition to the links to 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 the .htaccess file, you can specify files that will be protected from hotlinks. It prescribes 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 execute 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 web sites, and for other useful operations that allow you to competently manage the development and support of various projects.

Similar articles





Trending Now






Copyright © 2018 Theme powered by WordPress.