Htaccess (encoding): configuration, examples of use

Every person surfing the Internet has come across such web pages and sites that do not display correctly. For example, opening a website, the user sees instead of clear text various squiggles, among which you can distinguish hieroglyphs, arcs, symbols and other signs. Most likely to see this when you go to any Chinese or Japanese site. All this is due to the fact that other settings in the .htaccess file are used there. The coding in those countries is different. A correctly configured file will make it possible to avoid the appearance of such obscure characters on the site.

What is encoding needed for?

htaccess encoding

For the site there is a basic encoding called Default Charset. It is included in a special .htaccess file. Encoding is necessary in order to correctly determine the rule according to which characters will be converted to code values.

A couple of decades ago, the encodings were very small. They included no more than seven bits of information. An example would be ASCII encoding. Now there are a very large number of them. Among them are the most popular UTF-8, Windows-1251.

The bottom line is that documents with different encodings will look different. For this reason, 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 obscure characters.

Basic encodings

htaccess file

In .htaccess, you can specify a parameter by which the main encoding for the entire web portal will be determined. In addition, you can prescribe other encodings, which will allow them to correctly display on all pages of the site. Web developers in .htaccess encoding usually indicate two types - UTF-8 and Windows-1251. They are the defaults used by a large number of browsers and text editors.

To avoid the formation of squiggles that cannot be parsed, you must either configure the main encoding of the entire site using the Default Charset parameter, as well as prohibit transcoding of files in .htaccess or register all the necessary encodings. After all, the main problem is that, for example, the user can send some data in a completely different encoding, which the server will not be able to read. Such actions must be warned in advance.

How to set up site encoding in .htaccess file

The default Charset encoding, which is the key encoding processed by the default server and sent to the browser, is specified using the AddDefaultCharset option. It is needed in order to add the default encoding setting. When sending HTTP headers , a rule is written in them that tells the browser in which encoding all characters on the site should be considered. That is why the correct encoding must be specified in the Content-Type section. This is the .htaccess setting.

The above option is the main necessary directive in order to configure the encoding of the site in the file. This command is able to finally solve the problem associated with scripts that, by default, use encodings unknown to Russian-language browsers, and not those that are needed. If you did not register a parameter, the browser would give an error. Otherwise, all documents are automatically transcoded. Keep in mind 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.

You can get rid of encoding problems by activating automatic document transcoding. To do this, a special option CharsetSourceEnc is included in the .htaccess file. After it is necessary to prescribe the main encoding. All others will be transcoded into it. If for one reason or another it will be necessary to remove the encoding, then write the CharserDisable On command in the file.

Setting another encoding for individual sections of the site

htaccess setup

This does not happen often, but sometimes it becomes necessary for one page or the whole part of a section to be displayed in another language. Accordingly, another encoding is needed. If the .htaccess file is located in the root directory with the prescribed encoding, all pages of the site will be displayed in it. The result may be that a person goes into a foreign section of the website, and there instead of text in the language he needs, a group of incomprehensible characters and letters will be displayed.

In order to deal with this problem, you need additional .htaccess configuration. It is necessary to create one more same file and add the AddType command in it, in which additional encoding is indicated. This file can act on the entire website as well as on individual pages. It depends on where the .htaccess file will be located. If you place it inside a folder with a specific site language, its configurations will work only on this part. The rest will function according to the rules of the first .htaccess, which is located in the root directory of the site.

Redirect with .htaccess

htaccess redirect

Many website 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 running changes. It happens that initially the site was static and no one cared about the logical addresses. Redirection required (.htaccess requires changes).

Redirecting is also needed if the administrator wants to maintain the position of the site’s pages in search engine results. After all, raising the rating from scratch is a difficult task.

What is needed for such a diversion to be organized? .htaccess should be used on hosting. The redirect in it is carried out using the option Redirect 301, after which the address of the new page is indicated.

Access limitation

htaccess error

In order to limit access to the entire site, its sections or certain files that are on the server, .htaccess is also used. Access is denied so that files are not accessed by those users who might ruin something.

The following commands are used:

  • To deny access to the entire site, the deny from all command is written in the file.
  • To deny access from a specific ip address, use the deny from ip_user_address command.

File protection

htaccess access

Most likely, each user met absolutely identical articles on different websites. Most often it happens that they are simply stolen from other resources. Of course, search engines are struggling with such a phenomenon as duplication of pages, but very often they do not quite correctly choose the source. For copywriters and programmers, this is a very serious problem. Indeed, in order to write quality content, it takes a lot of time and effort. And then the search engine imposes sanctions on the copied material. It is unpleasant. Often images are also stolen with text. At the same time, a large number of such thieves do not even download images from other people's resources. In order not to overload their servers, they simply indicate links to images from the original source. This link insertion is called a hotlink. In addition to links to pictures, they can be inserted, pointing to the files that are available for download. For the content author, this phenomenon is extremely unpleasant. The source server is also overloaded because of them.

In the .htaccess file, you can specify files that will be protected from the hotlink. It prescribes extensions to which access will be denied. You can also set a password for hotel files or entire groups. Then the .htaccess file is placed in the directory that you want to close access to.

Error diagnosis

htaccess forbid

It happens that an error occurs with the code "500" after editing or placing the .htaccess file. This error appears due to flaws in the file itself. You can find out the reasons in a file located in the domain directory in the folder with reports.

Also, if what is specified in .htaccess fails, 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 the encoding of websites, and for other useful operations that allow you to competently manage the development and support of various projects.

Source: https://habr.com/ru/post/C29801/


All Articles