This page has been robot translated, sorry for typos if any. Original content here.

Redirects in htaccess and scripts (301/302)

Редиректы в htaccess (301/302)

Error 301 (301 Permament Redirect) , returned when accessing a specific page address, means that the site has been permanently transferred to a new address, also indicated in the HTTP header. Both users and search bots coming through the browser will be redirected to the new address, while for the search engines all the properties of the old address (page) will be transferred to the new URL.

Redirect 301 (moved permanently) is the best way to maintain search engine rankings when transferring it to a new domain or changing the content management system. With a 301 redirect, the old and new addresses will be glued together: parameters like PageRank and TCI, as well as the weight of the page and the reference weight of the old address will be transferred to the new URL.

Below are the most commonly used rules for configuring the .htaccess file for a 301 redirect.

Внимание In the rules: % {QUERY_STRING} - denotes the URL fragment after the question mark (setting values ​​for CGI parameters).

Внимание The triggering of a rule for a redirect is determined by whether the URL of the page falls under this rule or not.

Внимание For the meaning of certain designations (^, $, NC, etc.), see the memo at the bottom of the page .

Внимание All rules are executed in direct order in the .htaccess file and the rule written later will be executed later.

Внимание It is better to place all the rules after two lines:

  Options + FollowSymLinks
 RewriteEngine On 

301 redirect from a domain without WWW to a domain with a WWW prefix (the main mirror is a domain with www)

  RewriteCond% {HTTP_HOST} ^ site \ .ru $ [NC]
 RewriteRule ^ (. *) $ Http://www.site.ru/$1 [R = 301, L] 

From a domain with a WWW prefix on without (the main mirror is a domain without www)

  RewriteCond% {HTTP_HOST} ^ www.site \ .ru $ [NC]
 RewriteRule ^ (. *) $ Http://site.ru/$1 [R = 301, L] 

Standard redirection from one static page to another

  Redirect 301 / was.php http://www.site.ru/new.php 

At the same time, the new address must be specified completely with http and domain name.

In some cases redirect via RewriteRule is useful.

  RewriteRule ^ dir / dir-new / $ 1 [R = 301, L] 

301 redirect for a page with GET parameters

Let's say the page address is: h ttp: //www.site.ru/dir/index.php? IBLOCK_ID = 1 & SECTION_ID = 111 then to configure 301 redirects to a new address, you need to use the following rule:

  RewriteCond% {QUERY_STRING} ^ IBLOCK_ID = 1 & SECTION_ID = 111 $ [NC]
 RewriteRule ^ dir / index \ .php $ / new / sef /?  [R = 301, L] 

If one (or several) of the GET parameters is not specified (s) or can have an arbitrary value (in our example it is SECTION_ID ), you can use the following code:

  RewriteCond% {QUERY_STRING} ^ IBLOCK_ID = 1 & SECTION_ID = (. *) $ [NC]
 RewriteRule ^ dir / index \ .php $ / new / sef /?  [R = 301, L] 

301 redirect only site.ru/index.php addresses (without GET parameters) to the main site.ru mirror

  RewriteCond% {REQUEST_URI} /index.php
 RewriteCond% {QUERY_STRING} ^ \ z
 RewriteRule ^ (. *) $ Http://site.ru/?  [R = 301, L] 

301 redirect all addresses with index.php and GET parameters to pages with only GET parameters (cut into url index.php)

Example: type site.ru/index.php?n=1 on site.ru/?n=1

  RewriteCond% {REQUEST_URI} /index.php
 RewriteRule ^ (. *) $ Http://site.ru/ [R = 301, L] 

301 redirect url with GET parameters (dynamic URL) to static

Option 1 (simple address with GET parameter)

  RewriteCond% {QUERY_STRING} ^ id = 229
 RewriteRule ^. * $ / Supermodel /?  [R = 301, L] 

Option 2 (from page and GET parameter)

  RewriteCond% {REQUEST_URI} / test /
 RewriteCond% {QUERY_STRING} ^ id = 229
 RewriteRule ^. * $ / Supermodel /?  [R = 301, L] 

301 redirects for a specific file, not a whole folder

If you want to set up redirection only for the address http://www.site.ru/dir/ , but at the same time that the page http://www.site.ru/dir/index.php?IBLOCK_ID=1 opens at the old address, you need use the special character $ in the rule.

  RewriteRule ^ dir / $ http://www.site.ru/new-dir/ [R = 301, L] 

All pages of one domain to the main page of another domain

  RewriteCond% {REQUEST_URI} (. *)
 RewriteRule ^ (. *) $ Http://site.ru/ [L, R = 301] 

Each page of one domain to the same url of another

  RewriteCond% {REQUEST_URI} (. *)
 RewriteRule ^ (. *) $ Http://site.ru/$1 [L, R = 301] 

How to deal with domains in the zone of the Russian Federation?

For domains in the zone of the Russian Federation all the same rules apply, but only all Cyrillic characters must be replaced with an alternative code (it is in Latin). In particular, the zone itself. rf is converted to. xn - p1ai .

301 redirect from domain to domain

  RewriteCond% {HTTP_HOST} ^ old-site \ .ru $ [NC]
 RewriteRule ^ (. *) $ Http://www.site.ru/$1 [R = 301, L] 

301 redirect for a domain in the zone of the Russian Federation

  RewriteCond% {HTTP_HOST} ^ xn -... \. Xn - p1ai $ [NC]
 RewriteRule ^ (. *) $ Http://www.site.ru/$1 [R = 301, L] 

Setting forwarding to folders with a slash at the end of / (add a slash at the end)

  RewriteCond% {REQUEST_FILENAME}! -F
 RewriteCond% {REQUEST_URI}! \ .. {1,10} $
 RewriteCond% {REQUEST_URI}! (. *) / $
 RewriteRule ^ (. *) $ Http://www.site.ru/$1/ [L, R = 301] 

301 redirect from pages with a slash on without a slash (entire site)

  RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! [^ \ /] $
 RewriteRule ^ (. *) \ / $ / $ 1 [R = 301, L] 

Setting forwarding to folders without a slash (remove the slash at the end)

  RewriteCond% {REQUEST_FILENAME}! -D
 RewriteCond% {REQUEST_URI} ^ (. +) / $
 RewriteRule ^ (. +) / $ Http://www.site.ru/$1 [R = 301, L] 

301 redirect from pages without a slash to a slash (often in CMS systems it is installed automatically)

  RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteRule ^ (. * [^ \ /]) $ / $ 1 / [R = 301, L] 

One (and not two consecutive!) 301 redirect to no www and with a slash on the end of the page address

  RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) $ Http: //% 1 / $ 1 / [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! [^ \ /] $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) $ Http: //% 1 / $ 1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) $ Http: //% 1 / $ 1 / [L, R = 301] 

One (and not two consecutive!) 301 redirect to c www and with a slash at the end of the page address

  RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) $ Http: //www.%1/$1/ [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) $ Http: //www.%1/$1/ [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! [^ \ /] $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) $ Http: //www.%1/$1 [L, R = 301] 

One (and not two consecutive!) 301 redirect to c www and without a slash on the end of the page address

  RewriteCond% {REQUEST_URI} ^ \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) $ Http: //www.%1/$1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI} \ / $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) \ / $ Http: //www.%1/$1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) $ Http: //www.%1/$1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI} \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) \ / $ Http: //www.%1/$1 [L, R = 301] 

One (and not two consecutive!) 301 redirect to without www and without a slash at the end of the page address

  RewriteCond% {REQUEST_URI} ^ \ / $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) $ Http: //% 1 / $ 1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI} \ / $ 
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) \ / $ Http: //% 1 / $ 1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI}! \ / $
 RewriteCond% {HTTP_HOST} ^ www \. (. *) $
 RewriteRule ^ (. *) $ Http: //% 1 / $ 1 [L, R = 301]
 
 RewriteCond% {REQUEST_URI}! \?
 RewriteCond% {REQUEST_URI}! \ &
 RewriteCond% {REQUEST_URI}! \ =
 RewriteCond% {REQUEST_URI}! \.
 RewriteCond% {REQUEST_URI} \ / $
 RewriteCond% {HTTP_HOST} ^ ([^ www]. *) $
 RewriteRule ^ (. *) \ / $ Http: //% 1 / $ 1 [L, R = 301] 

301 redirect from domain to folder on another domain

  RewriteCond% {HTTP_HOST} ^ si-te \ .ru $ [NC]
 RewriteRule ^ (. *) $ Http://www.site.ru/si-te/ [R = 301, L] 

Redirection from all domain files, except the bitrix admin folder

  RewriteRule ^ bitrix // / bitrix / admin / [L, R = 301]
 RewriteRule ^ (. *) $ Http://www.newsite.ru/new/ [L, R = 301] 

Redirect all files in the folder to the specified file

  RewriteRule ^ dir (. *) $ /New-file.php [L, R = 301] 

Redirecting files from a specified folder except a specific file

  RewriteRule ^ dir / no-file.html / no-file-new.html [L, R = 301]
 RewriteRule ^ dir (. *) $ /All.php [L, R = 301] 

Change pages from html extensions to php extension

  RedirectMatch 301 (. *) \. Html $ http: //www.new-site.ru$1.php 

Setting the type of index page (php, html, htm and others)

Specifies the order of loading the types of the index file that are in the root of the directory.

  DirectoryIndex index.html index.php index.htm index.shtml 

Redirect from php index page to the folder itself (root)

  RewriteCond% {THE_REQUEST} ^ [AZ] {3.9} \ / index \ .php \ HTTP /
 RewriteRule ^ index \ .php $ http://www.site.ru/ [R = 301, L] 

Redirect from a subdomain to the primary domain of the second level

  RewriteCond% {HTTP_HOST} ^ test.site.ru $ [NC]
 RewriteRule ^ (. *) $ Http: //site.ru% {REQUEST_URI} [R = 301, NC, L, QSA] 

Redirect for a given file in different directories (folders)

The code allows you to put 301-redirect from all folders of the form http://site.ru/***/uniqe-file.html on one file in the root /unique-file.html. It is useful when altering the site and changing links.

  RewriteRule [^ abc] /unique-file.html /unique-file.html [R = 301, L] 

If you want to create an NC copy of any dynamic page, then this can also be implemented using .htaccess

The code allows you to create a copy of the page with the relative address /studio/news/detail.php?ID=230354&PAGEN_2=11 at / testovyi / test /.

  RewriteRule ^ testovyi / test /? $ /Studio/news/detail.php?ID=230354&PAGEN_2=11 [NC, L] 

Specifying the path to the 404 error file using .htaccess

Attention, it is important that the server response code for 404 error was exactly 404. The path to the file is specified using the following line:

  ErrorDocument 404 /404-for-me.php 

Redirects on the Apache server

Внимание For sites that do not use the Apache server, similar 301 redirects are easily configured using PHP.

  <? php
 header ("HTTP / 1.1 301 Moved Permanently");
 header ("Location: http://www.site.ru/dir/");
 exit ();
 ?> 

Внимание Optimally adjusting all redirects to the destination page immediately (without intermediate redirects, in one step), this improves their perception by the search engines and users.

If you want to set up a redirect only for some USER_AGENTs, but not for all users

  RewriteCond% {HTTP_USER_AGENT} (iPad | ipad | iphone | iPhone | ipod | iPod | android | midp | j2me | symbian | series \ 60 | symbos | windows \ mobile | windows \ ce | ppc | smartphone | blackberry | mtk | bada | windows \ phone) [NC]
 RewriteRule (. *) Http://mobile.site.ru/ [L, R = 301] 

If you need to set up a redirect for all search engines (a list of their USER_AGENTs is presented)

  RewriteCond% {HTTP_USER_AGENT}! (Accoona | ia_archiver | antabot | ask \ jeeves | baidu | dcpbot | eltaindexer | feedfetcher | gamespy | gigabot | googlebot | gsa-crawler | grub-client | gulper | slurp | ih-ih-ih-shi | googlebot | gsa-crawler | grub-client | gul-crawler | googlebot | gsa-crawler | grub-client | gulab | pagebull | scooter | w3c_validator | jigsaw | we see the yaho!
 RewriteRule (. *) Http://no-search.site.ru/ [L, R = 301] 

Some simple examples

  Redirection from www.site.ru/component/content/?view=featured to www.site.ru/
 RewriteCond% {QUERY_STRING} ^ view = featured $ [NC]
 RewriteRule ^ component / content /? $ /?  [R = 301, L] 
  Redirection from www.site.ru/index.php?idc=4&marea=6 to www.site.ru/
 RewriteCond% {QUERY_STRING} ^ idc = 4 & marea = 6 $ [NC]
 RewriteRule ^ index \ .php $ /?  [R = 301, L] 

Remove all GET parameters after question mark (?)

  RewriteRule (. *) $ 1?  [R = 301, L]
 Position after: RewriteBase / 

The site.ru main page always has its full duplicate at site.ru/index.php

  Redirect 301 /index.php http://site.ru/ 

Or

  RewriteCond% {THE_REQUEST} ^ [AZ] {3.9} \ / index \ .php \ HTTP /
 RewriteRule ^ index \ .php $ http://site.ru/ [R = 301, L] 

If your site has several names, but you want the users to always see the main name of the site in the address bar, use the following lines immediately after RewriteEngine On:

  RewriteCond% {HTTP_HOST}! ^ Site.ru $
 RewriteRule ^ (. *) Http://site.ru/$1 [R = 301, L] 

301 redirect to the end of .html (for those who have this suffix included) will redirect from site.ru/article and site.ru/article/ to site.ru/article.html

  RewriteCond% {REQUEST_URI} (. * / [^ /.] +) ($ | \?)
 RewriteRule. *% 1.html [R = 301, L]
 RewriteRule ^ (. *) / $ /$1.html [R = 301, L] 

Or

  REDIRECTMATCH 301 (. * / [^ /.] +) ($ | \?) $ Http: //site.ru$1.html 

Redirect from .html to without .html, i.e. from site.ru/article.html to site.ru/article (for those who first included .html, and then decided to get rid of it)

  RewriteBase /
 RewriteRule (. *) \. Html $ $ 1 [R = 301, L] 

Or

  REDIRECTMATCH 301 (. *) \. Html $ http: //site.ru$1 

Redirect for pages with parameters, for example from the site.ru/blog?limitstart=0 page on site.ru/blog

  RewriteCond% {QUERY_STRING} ^ limitstart = 0
 RewriteRule ^ blog http://site.ru/blog?  [R = 301, L] 

So that all pages of the old section are redirected to the same pages of the new section only, for example site.ru/blog/raznoe/article to site.ru/blog/article

  RewriteRule ^ blog / raznoe /(.*)$ http://site.ru/blog/$1 [R = permanent, L] 

301 redirect from the address without a slash to a slash, that is, from site.ru/article to site.ru/article/

  RewriteCond% {REQUEST_URI} (. * / [^ /.] +) ($ | \?)
 RewriteRule. *% 1 / [R = 301, L] 

Redirect from slash to no slash at the end, i.e. from site.ru/article/ to site.ru/article

  RewriteRule ^ (. *) / $ / $ 1 [R = 301, L] 

Another option is to get rid of the trailing slash at the end

  RewriteCond% {REQUEST_FILENAME}! -D
 RewriteRule ^ (. +) / $ / $ 1 [R = 301, L] 

The option to get rid of the slash for the pages with parameters, on the example of pages with pagination site.ru/categoriya?start=5/

  RewriteCond% {QUERY_STRING} ^ start = (\ d +) /
 RewriteRule ^ (. *) / $ 1? Start =% 1 [R = 301, L] 

At first, they forgot to include SEO in global settings, and then included, as a result, many documents in the index with /index.php in the address.

By the same principle, you can get rid of any nesting, for example, redirect from site.ru/ru/catalog to site.ru/catalog (/ ru / is removed).

  RewriteRule ^ index.php /(.*)$ http://mysite.ru/$1 [R = permanent, L] 

Deny access for bad bots

  SetEnvIfNoCase User-Agent "^ Baiduspider" bad_bot
 SetEnvIfNoCase User-Agent "^ MSNBot" bad_bot
 SetEnvIfNoCase User-Agent "^ Baiduspider" bad_bot
 SetEnvIfNoCase User-Agent "^ Ezooms" bad_bot
 # continue the list yourself, specify the user agent of bad bots
 Order Allow, Deny
 Allow from all
 Deny from env = bad_bot 

Or robots.txt gives, for the rest 404 (for the user agent - Baiduspider and Ezooms)

  RewriteCond% {HTTP_USER_AGENT} \ b (Baiduspider | Ezooms) \ b [NC]
 RewriteCond% {REQUEST_URI}! ^ / Robots \ .txt [NC]
 RewriteRule. * - [R = 404] 

Various other ways to redirect

Below are similar different settings rules for 301 redirects.

Redirect using script (send headers)

Redirect requests can also be carried out using scripts, sending the necessary headers to the client.

  HTTP / 1.1 301 Moved Permanently
 Location: http://www.newdomain.ru/newdir/newpage.htm 

Javascript redirect

This is where there is no limit to creativity and the possibility of "to izgolyatsya." JavaScript redirection options are more often implemented using the setTimeout function ('function', delay) .

For example, automatically make a Click on the "Submit" button of the "searchform" form in 0.1 seconds after downloading the code

  setTimeout ('document.forms ["searchform"]. Submit.click ()', 100); 

You can hang any action on the "Submit" button, for example, open a new url in this window. By the way, such redirects are more common in the organization of Doorways (DorWay) - the User’s browser will be redirected to another page, and the search robot that does not “understand” JavaScript will index the ETA unavailable to the User. On it, the portal builders post text stuffed with the "necessary" keywords.

Simply redirect to another page — insert the JavaScript code after <body>:

  location = "http://www.new.domain.ru"; 

or

  document.location.href = "http://www.new.domain.ru"; 

or

  window.location.reload ("http://www.new.domain.ru"); 

or

  document.location.replace ("http://www.new.domain.ru"); 

In the latter case, it will not be possible to return to the page that performed the redirection, since the page address is erased from history (which is often required).

If you need a time delay, you can arrange location = "http://www.new.domain.ru"; as a function and insert it into setTimeout ('function ()', delay_in_milisek); .

How different search engines may relate to such a redirect, remains on their "conscience", so for the purposes described here it is better not to use them. Most browsers will handle this redirect as it should be, while the user can be shown additional information why he is moved to a different address.

Since it may take several weeks or months to transfer the PR of an old site (page) to a new one, do not destroy the old domain name, site or page until this happens.

PHP redirect

  <? php
 header (“HTTP / 1.1 301 Moved Permanently”);
 header (“Location: http://www.newdomain.ru/newdir/newpage.htm”);
 exit ();
 ?> 

ASP redirect

  <% @ Language = VBScript%>
 <% 
 Response.Status = “301 Moved Permanently”
 Response.AddHeader “Location”, “http://www.new-url.com”
 response.end
 %> 

ASP.NET redirect

  <script runat = “server”>
 private void Page_Load (object sender, System.EventArgs e)
 {
 Response.Status = “301 Moved Permanently”;
 Response.AddHeader (“Location”, “http://www.new-url.com”);
 }
 </ script> 

ColdFusion redirect

  <.cfheader statuscode = “301” statustext = “Moved permanently”>
 <.cfheader name = “Location” value = “http://www.new-url.com”> 

JSP (Java) Redirect

  <%
 response.setStatus (301);
 response.setHeader (“Location”, “http://www.new-url.com/”);
 response.setHeader (“Connection”, “close”);
 %> 

CGI PERL

  $ q = new CGI;
 print $ q-> redirect (“http://www.new-url.com/”); 

Ruby on rails

  def old_action
 headers [“Status”] = “301 Moved Permanently”
 redirect_to “http://www.new-url.com/”
 end

Redirecting nginx

  if ($ host = 'www.domain.com') {
 rewrite ^ (. *) $ http: //domain.com$1 permanent;
 } 

Memo on used symbols and symbols

The RewriteCond line is the condition for executing the RewriteRule rule. If the condition is met, then the redirect is triggered.

Rules can be set using regular expressions.

Special characters used in the rules and their meanings.

  • ^ - special character of the beginning of the line;
  • $ - special character of the end of the line;
  • ! - special character of negation;
  • . - point, replaces any character, but only one;
  • () - grouping;
  • \ - "screening" slash, the next character after it is considered normal, and not a special character.

Modifiers are used after ordinary, special characters or their groups and allow you to expand the possibilities of patterns for triggering rules.

  • ? - the symbol is repeated 0 or 1 time;
  • + - repeats from 1 to 65536 times;
  • * - repeats from 0 to 65536 times.

Flags, ask additional. options for the rule to be used. Listed in square brackets separated by commas, say [NC] or [R = 301, L].

  • NC - NoCase flag, which disables the register of characters when the rule is triggered
  • R - the Redirect flag, performs the process of stopping the URL change and returns the result. The most commonly used value is R = 301, but others are possible for temporary redirections (302, MOVED TEMPORARY).
  • L - Last flag, stops forming URLs and the string is considered final.

Syntax for regular expressions

. - Point replaces arbitrary character.

[abc] - indicates a list of characters that match the letters a, b, or c.

[^ abc] - a list of characters that are not included in the specified range. Matches any character except a, b, or c.

* - means that the preceding character can be repeated (0 or more times).

[abc] * - the command will find the characters in succession from the given set.

[^ abc] * - exactly the opposite.

. * - replaces absolutely any character set. ". *" - will find all the substrings between quotes.

^ - the beginning of the line (if used at the beginning of the expression).

$ - marks the end of the line.

\ w - letter, number or underscore _.

\ d - replaces any digit.

\ D - replaces any character, but not a number.

[0-9] - replaces any digit.

[az] - any letter from a to z (the entire Latin character set) in lowercase.

[AZ] - Any letter from A to Z in the upper case.

[a-zA-Z] is any letter from a to Z in any case.

[aZ] - the same.

All about redirect

Disclaimer: Materials for this article were taken from the vast expanses of the Internet (Runet and Bourgeois) and personal experience. Some points seem controversial and are now being tested in practice, so the article’s information will be supplemented and refined.
** In connection with the frequent reports about Google 302 Pagejacking , use Redirect 301, and not 302 - this topic is still under development.

Where did this www come from?

On the Internet, you can find a lot of recommendations like “merge” domain.ru and www.domain.ru. But besides how to do this, it would be helpful to understand a little bit also why. This would help to choose the appropriate solution from the proposed variety.

First, it is important to understand that www.domain.ru is technically the same as sub.domain.ru , although this state of affairs exists for a completely different reason.

Ten or twelve years ago, the World Wide Web was just one small part of the Internet, and the fastest PC was based on 386 chips. They were not very fast and could not handle most of the downloads, so there was a need to place different “parts” of the Internet on separate machines.

For example, the Apache server was located on one computer, the mail server on another, and the FTP server on another. Each of the computers responded to a different IP address, but to the same domain name. Within this domain name, computers were differentiated according to the service provided (which was then called the "machine name"). Thus, the names of servers on the Internet began with the “machine name” according to the service provided to it: www.domain.ru , mail.domain.ru , and ftp.domain.ru . (The “old-timers” of the Internet, they probably also remember such an archaic service as gopher.domain.ru , in the settings of IE it still remains).

Today's computers, of course, are much more powerful, and we can put all the different “parts” of our Internet services in the same “box” (the Head & Sholders principle - “two in one bottle” was applied long before the mass appearance of “dandruff” in Russia :) .

Indeed, we often set up several hundred domains, each with its own set of services (http, ftp, mail ...), on the same server. Therefore, at present, the www prefix is ​​an “antique” and can be ignored. The trouble is that a lot of software and large directories (for example, Yahoo) automatically substitute www.domain.ru , even if you type domain.ru , plus we have several billion people who automatically type www in front of any name domain

This excursion into the History, at least for me, seems interesting, but the only important thing about it is that technically www.domain.ru - just like sub.domain.ru - are considered completely different objects regarding domain.ru , but for the reasons stated above, usually www.domain.ru and domain.ru usually have to show the same page, unlike sub.domain.ru .

The user (Surfer) or search engine (spider), when requesting the page, will receive the same 200 OK response code and will not be able to differentiate the domains with and without www .

Creating an alias for www

If someone shouts "Yuri!" in a crowded room, he's probably going to get my attention. If someone shouts "Savilov!" in the same room, I'm going to answer that as well. Because these two names, although completely different, generally indicate the same person. Simplifying a little, we can say that “Yuri” is essentially a pseudonym (alias) for “Saviles”. If someone in that room approaches and calls me as “Oleg,” I will probably look around, find “Oleg,” and point it out to what this someone is looking for (if this someone is not that pretty person, so I could lie :) . “Yuri” and “Oleg” are two different names that indicate two different people, so I have to redirect the person’s request to where he will be answered according to his request. At 99.9% of all server configurations, domain.ru and www.domain.ru point to exactly the same disk space. Each is just a pseudonym for another. Using forwarding to refer to the same disk space has the same meaning as when someone calls me "Yuri" and I answer: "no, you need" Savile, "it's me."

Alias and Redirect are not the same, although it is often possible to replace one with another. The creation of an alias is generally achieved on two levels. At the domain name system (DNS) level, we must always create an entry for each, usually a CNAME for each, or perhaps a CNAME for the primary and a secondary entry. If the domain in question is on a static IP address, that’s all we need to do to create an alias. Most of us, however, share the IP address with other domains, so we must move beyond the domain name system (DNS) level to the network server level in order to complete the alias configuration. Using Apache, for example, create an entry point in the hpptd.conf configuration file

 ServerName domain.ru
 DocumentRoot / home / domain / www
 ServerAlias ​​www.domain.ru

Naturally, you first need to associate a non-www version with a www version via an entry in the DNS server like:

For top level domain:

  IN A 192.xxx.xxx.xxx
 www IN A 192.xxx.xxx.xxx 

For the info subdomain in the top level domain:

  info IN A 192.xxx.xxx.xxx
 www.info CNAME info 

That is all that needs to be done. Surfer or Spider, going either to domain.ru or www.domain.ru will get exactly the same pages. Our problem, however, is that the requested URL does not change, and the requested page will respond 200 OK, regardless of whether we are accessing the domain with www or without it. If, for Surfer, this is, in general, indifferent, then this cannot be said with respect to the search server (Spider), which should, at least initially, process both domain.ru and www.domain.ru as separate objects, even with that we know that they are pseudonyms. Google is the first search server to use complex dual filters and the logic necessary to eventually “merge” domain.ru and www.domain.ru into a single object. Unfortunately, there is some small emphasis in that sentence on the word "ultimately." If you use the domain name system (DNS) or ServerAlias ​​aliases, then Google will eventually recognize domain.ru and www.domain.ru as a single entity and will only show one site in the SERP. Unfortunately, this usually takes several months, and if you frequently change the content of the site, since the Spider indexes domain.ru/page.htm and www.domain.ru/page.htm at different times - it will receive different content of pages from www and without). Then these few months can take a year or more.

Why do I need to "combine" domain.ru and www.domain.ru?

Many "serious" sites use only the domain with the www prefix (for example, most of the sites with PR 10. If you try to access w3.org or adobe.com, you will see that your browser will immediately be redirected to the site with www . This not just a pseudonym, because the location of your navigation actually changes to a new domain from www.J.I believe that Google treats this normally, not only because “serious” websites do this, but Google itself does this (if you don’t believe it, type google.com in the browser.) Interestingly, the majority of "serious" sa It uses forwarding (Redirect) 302. Only a few, like w3.org, return a status code of 301. Apparently, Google treats Redirect 301 and 302 in the same way.

Only for some reason, Google for some reason believes that the site should be with the www prefix: you can compare the search request for the "muzzle" of the site narod.ru to Google - the difference will be noticeable - without the www prefix, Google will include all subdomains in the issue result. But, since there are precedents when a website without www has a PR higher than a website without www, it means that Google does not discriminate against any of them.

Therefore, instead of relying on search engine pseudonyms and filters (Spiders), someone came up with the bright idea of ​​redirecting one alias to another alias, which is essentially a redirect to itself. This is one of those things that is done basically because CW search engines exist and should, therefore, help avoid splitting backlinks. However, there may be other reasons requiring mandatory redirection: since technically http://mysite.ru/ and http://www.mysite.ru/ are DIFFERENT sites, PHP sessions cannot be transferred from one to another. This is another reason for rewriting the URL in the browser by redirecting to the alias, especially if you have only one SSL certificate for the www.mysite.ru domain ...

Technical Options Redirect

Now let’s try to answer the question why the redirect recommendations often suggest mod_rewrite and RedirectMatch in the .htaccess file (requiring certain machine resources to execute them), and less often a simpler solution. Why can't we eliminate all complex Regular Expressions and just add the line "Redirect 301 / http://www.domain.ru" to our .htaccess (for the hpptd.conf option above)? Since domain.ru and www.domain.ru are pseudonyms and point to the same directory, any .htaccess directives in this directory will apply to BOTH domains. Simple Forwarding, on some server platforms, will lead in a "vicious circle" and, ultimately, a crash. Let's try to slightly change the server configuration:

 ServerName domain.ru
 DocumentRoot / home / domain / www
 ServerName www.domain.ru
 DocumentRoot / home / wwwdomain / www

Note: in this configuration, domain.ru , and www.domain.ru point to different folders on the server and therefore not aliases. We can now place the .htaccess file in the / home / wwwdomain / www folder without affecting anything in the / home / domain / www folder and use the simpler Redirect command to achieve the goal. The problem is solved, but, unfortunately, we are wasting a bit of disk space, so you will not often see this configuration. However, this configuration gives you another option:

 ServerName domain.ru
 DocumentRoot / home / domain / www
 ServerName www.domain.ru
 Redirect 301 / http://domain.ru/

We still configure domain.ru and www.domain.ru as separate objects (not aliases), but instead of defining the DocumentRoot for the second object, we insert simple redirection . We avoid the contradictions of trying to redirect a pseudonym without creating it. The disadvantage of this method is that it requires more work for the host administrator. We need to define two blocks of records instead of one, but more importantly, we must support both blocks “in unison”. The advantage is that every single page request made to your domain results in a read operation for .htaccess and parsing the file. Now add all those regular expressions that will be used for each individual page request, and then multiply all this 200 - 500 other domains that live on the server.

The redirection placed in httpd.conf does not require any RegExp and more importantly it is read and parsed only ONE time when the Apache server is started. This, in my opinion, is the most elegant solution , since avoids the creation of aliases and minimizes server load.

It remains to clarify the question: what is more literate than Redirect 301 or 302 ? As noted above, many “serious” websites calmly use Redirect 302 to “merge” the domain with www and without and Google also calmly accepts this. But not the fact that other search engines will do the same. Still, the RFC has not been canceled: the code “301” means that the page has been moved permanently - “moved permanently” , the code “302 means that the temporary move has been moved , therefore the use of the code should depend on the purpose of moving the page. It should also be understood that many browsers, receiving the answer "301 - moved permanently," can automatically reconfigure bookmarks to a new page. Similarly, it’s not a fact that, the same Google, will promptly transfer the PR to the page moved by the redirect 302, regarding it as “temporary” until both sites are mirrored.

Redirect to various SE (Search Engines):

In general, the redirect is perceived differently by various search engines. If you want to use a redirect to "merge" a www-version of a site with a non-www version, you should keep in mind the following remarks.

If on your site some links are set as www, and some like non-www, then you are probably interested in “combining” the weight of links to both versions of the site in terms of CYP / PR and reference ranking.

Redirect for Yandex

The fact is that Yandex combines links for sites that it considers mirrors, and a redirect from site.ru to www.site.ru will preclude Yandex from accessing site.ru and, therefore, it will not be considered a mirror with all the ensuing consequences. For splicing by Yandex, it is necessary that both site names are accessible (answered " 200 OK ") and have the same content.

Additionally, it is necessary to define the main mirror of the site with the Host directive in the Robots.txt file, for example:

  User-agent: *
 
Disallow:

User-agent: Yandex
Disallow:
Host: www.info.data-com.ru

* Literately put Host directives into a separate section only for a Yandex robot (there is information that Google either ignores a section in which directives it does not understand, or it does not work correctly);
** According to the robots.txt standard, each section of the "User-agent:" must contain at least one "Disallow:" directive, so the example contains an "empty" directive that does not prohibit anything. For your case, list your own limitations, if any.

Redirect for Google

Google normally understands Redirect, at least this can be judged by a recent post by Vanessa Fox in the official Google Sitemaps blog. In order not to lose this interesting text, I quote it completely:

Inside Google Sitemaps:
More about changing domain names
1/27/2006 09:27:00 AM
Posted by Vanessa Fox, Google Engineering

Recently, someone asked me to move from one domain to another. He had read that google recommends using a 301 redirect
to let you go. He wondered if Googlebot would follow
The 301 to the new site, see
think it was duplicate content (and therefore not index it). He wondered if a 302 redirect would be a better option.

He told him a 301 redirect. A 302 redirect tells Googlebot that the move is temporary
and that google should continue to index the old domain. A 301 redirect tells Googlebot that the move is permanent and that
Google should start indexing the new domain instead. Duplicate content, but as moved
content. And that's it.

He also wondered how he looked. He thought that a new site
could not be indexed to existing site. He noticed that he had noticed
it’s not a funeral. Googlebot learns about
new pages to crawl by sitemaps. If Googlebot already knows about a site,
since the site links to the new pages.

I & # 39; re told you to use the sitemap for
The Googlebot couldn’t have otherwise. He
he could not be able to update their links.

I’m not sure where to start
showing up in results. But letting Googlebot know about the site (using a 301 redirect and a Sitemap) is an important first
step in that process.

Here is a translation of the most significant parts of this text:

Recently, someone asked me about moving from one domain to another. He read that Google recommends using Redirect 301 to tell Googlebot about the move, but he wasn’t sure if he should do it.

I told him that Redirect 301 is exactly what he should do. Redirect 302 tells Googlebot that the move is temporary and that Google should continue to index the old domain. Redirect 301 tells Google Bank that the move is permanent and that Google should start indexing the new domain. Googlebot will consider the new domain not as duplicate content, but as transferred content.
. . .
Googlebot will find out about new pages for indexing links from other pages and from Sitemaps.
. . .

Here's another snippet from the Google Help Center on Redirect 301:

My URL has been changed.

You can’t make your transition.
is smooth.
First, you can redirect individuals to your new site. If your old URLs are redirect to your new site using HTTP 301 (permanent)
redirects, our crawler will discover the new URLs.
. . .
Findings from links to other sites. To preserve your rank, you want
to change your address. One way to find out yours
perform a link search.

Here is the translation of this fragment: -

"While we cannot manually change your URL in our search results, there are steps you can take to make your transition smooth. First, you can redirect people to your new domain. If your old URLs are redirected to your new domain Using HTTP 301, our spider will detect new URLs ";

- "Google's issuance lists are partly based on the ability to find a site from links from other sites. To save your ranking, you will want to tell those who link to you about changing your address."

And finally, another important piece from the Google Help Center on this topic:

Why does my site have two different listings in Google: http://site.com and http://www.site.com?

If we look at you
more accurately determine your site's PageRank. Redirect your http URL to your www URL using
a 301 redirect. Crawler discovers the change.
. . .
Please note:
site will not work. You can’t be manually add your site back to our index.

from which it follows that Google should combine PR sites on the 301 redirect.

Pay attention to the note to the last fragment. You will not be able to remove separately non-www version or www version from Google index - you will delete both versions at once for 180 days.

Afterword

To check the gluing of the site by Yandex and the "merger" of the TCI, another method has been applied (it has been launched on this site since March 2006). The site responds to the name with www and without www. The pages of the site check the HTTP_USER_AGENT of the requesting page, and if this is a Yandex robot, then the answer is given to both of the referrals by the site name (with www and without) 200 OK and the page itself. For all others, the site is available only as www.info.data-com.ru, when accessing info.data-com.ru, Redirect 301 is made to www.info.data-com.ru. The cloaking method is built on the same technology - when search engines are shown one content of the page, and users are shown another. Cloaking is punished by search engines by excluding the site from their index and, as a result, from issuing.

To test the Google PR merge through Redirect 301, it has been installed on this site since February 2006, and if the RP www.info.data-com.ru and info.data-com.ru are equal, this fact can be considered confirmed. (links to www.info.data-com.ru and info.data-com.ru are put down approximately 50 to 50).

** If you decide to use a redirect for your site - do Redirect 301, and not 302. At least until Google 302 Pagejacking clarifies the situation - this article is still under development.