Настройка .htaccess

Configure .htaccess

Configure .htaccess

File .htaccess is an Apache configuration file. It allows you to specify how you want the server to queries the user to configure caching and compression, and also to restrict access to different areas of the site

Opportunities offered htaccess server configuration is too large to t in one article, but we not going to consider all the details of the Apache configuration, and consider only the basic settings .htaccess to reduce duplicate pages on the website.


First, on the website has a duplicate of the main mirror, the site opens with www without him.

This situation is bad because:

  • Confuses the user about the correctness of names website
  • Impairs memorizing the name of the website by the user
  • Prevents search engine robots to perceive the page (search engines consider pages with www and without www are different, and content, they mirrored)

To get rid of a duplicate of the main mirror in .htaccess needs to be written:

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

To tune in the primary mirror of the site name without www

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

To tune in the primary mirror of the title with www


Secondly, on the website has a duplicate of the head file of the site (index.html, index.php and etc.)

This situation is bad because:

  • Affect the form of the main page of the site (much nicer to see the site name without any index than take off)
  • Prevents search engine robots to perceive the page (search engines consider the website url with index without it as different pages, and content, they mirrored)

To get rid of duplicates of head file of the site needs to be written in .htaccess:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://crazysquirrel.ru/$1 [R=301,L]

Thirdly, the duplicates GET (parameters passed in the url after the ?)

This situation is bad because:

  • Affect the form of the main page of the site (much nicer to see the URL without GET parameters rather than take off)
  • Prevents search engine robots to perceive the page (search engines consider the website url with GET without it as different pages, and content, they mirrored)

To get rid of GET duplicates needs to be written in .htaccess:

RewriteEngine on
RewriteCond %{QUERY_STRING} !^$ [NC]
RewriteCond %{THE_REQUEST} \? [NC]
RewriteCond %{THE_REQUEST} !admin [NC]
RewriteRule ^(.*)?(.*)$ http://crazysquirrel.ru/$1? [R=301,L]

The General rule for getting rid of duplicates:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^crazysquirrel\.ru$ [NC]
RewriteRule ^(.*)$ http://crazysquirrel.ru/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://crazysquirrel.ru/$1 [R=301,L]
RewriteCond %{QUERY_STRING} !^$ [NC]
RewriteCond %{THE_REQUEST} \? [NC]
RewriteCond %{THE_REQUEST} !admin [NC]
RewriteRule ^(.*)?(.*)$ http://crazysquirrel.ru/$1? [R=301,L]

In data configurations:

  • RewriteEngine on — Directive on/off mechanism of transformation (written as one for the entire htaccess)
  • RewriteCond %{HTTP_HOST} !^crazysquirrel\.ru$ [NC] — defining the conditions for the transformation is applied only to the names of the site, distinct from the desired
  • RewriteRule ^(.*)$ http://crazysquirrel.ru/$1 [R=301,L] — conversion mechanism redirect the user to "the correct name of the site"
  • RewriteCond %{QUERY_STRING} !^$ [NC] — defines the condition for the transformation is applied only to address of which there are GET parameters
  • RewriteCond %{THE_REQUEST} \? [NC] — defines a condition for conversion only for addresses that have ?
  • RewriteCond %{THE_REQUEST} !admin [NC] — defines the conversion rules for addresses not with its composition the directory or file, the administrative panel of your website (if the site has no CMS or not work required GET parameters, this line can not write)
  • crazysquirrel.EN — the name of the website
  • admin — directory administrative panel of the site
  • index.php — the main file of the site
  • R=301  a flag that sets the http status code for the wrong site (301 — the flag of permanent change-of-address)
  • L — a flag indicating that the last rule
  • NC — a flag indicating that this rule is not case sensitive writing