Configure .htaccess

File .htaccess is the configuration file of Apache. It allows you to set the server response to user requests, configure caching and compression, as well as to differentiate access to different areas of the site

The possibilities offered by htaccess to server configuration is too big to fit in one article, but we are not going to consider all the details of Apache configuration, and we consider only the basic settings .htaccess to reduce duplicate pages on the website.


Firstly, the site contains a duplicate of the main mirror, the site opens with www and without it.

This situation is bad for those that:

  • Confuses the user about the correct name of the site
  • Impairs memorizing the name of the site user
  • Prevents search robots to perceive adequately the page (search engines consider pages with www and without www are different, and the content on them mirrored)

For getting rid of a duplicate of the main mirror .htaccess it is necessary to prescribe:

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

To configure as the primary mirror website name without www

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

To configure as the primary mirror of the name with www


Secondly, the site contains a duplicate of the head file of the site (index.html, index.php etc.)

This situation is bad for those that:

  • Degrades the main page of the site (much nicer to see the name of the site without index rather than take off)
  • Prevents search robots to perceive adequately the page (search engines consider website address with index and without it, as different pages and duplicate content on them)

For getting rid of duplicate of the head file of the site you must register .htaccess:

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

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

This situation is bad for those that:

  • Degrades the main page of the site (much nicer to see the URL without the GET parameters rather than take off)
  • Prevents search robots to perceive adequately the page (search engines consider website address with GET and without it, as different pages and duplicate content on them)

For getting rid of duplicates GET need to write 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 these configurations:

  • RewriteEngine on Directive is on/off conversion mechanism (written one for the whole htaccess)
  • RewriteCond %{HTTP_HOST} !^crazysquirrel\.ru$ [NC] — the definition of the conditions for applying the transform only to the name of the website that is different from the desired
  • RewriteRule ^(.*)$http://crazysquirrel.ru/$1[R=301,L] — converting mechanism redirect the user to the "correct site name"
  • RewriteCond %{QUERY_STRING} !^$ [NC] — defines the condition for the transformation is applied only to addresses that have GET parameters
  • RewriteCond %{THE_REQUEST} \? [NC] — defines the condition of conversion only for the addresses having ?
  • RewriteCond %{THE_REQUEST} !admin [NC] — defines transformation rules for addresses not having in its composition a directory or file, the administrative panel of the site (if the site has no CMS or its operation are not required GET parameters, this line can not write)
  • crazysquirrel.EN — the name of the site
  • admin — directory admin panel of the website
  • index.php the main file of the site
  • The R=301 flag that sets the http status code for the wrong site (301 — flag constant change')
  • L — a flag indicating that the last
  • NC — flag indicating that this rule is not important register writing
View and leave comments