Redirects in htaccess and Scripts (301/302)

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

The 301th error (301 Permament Redirect) returned when accessing a specific page address means that the site has been permanently moved to a new address, also specified 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 properties of the old address (page) will be transferred to the new URL.

Redirect 301 (moved permanently) is the best way to keep the site ranking in search engines when you move it to a new domain or change the content management system. When 301 redirects, the old and new addresses will be pasted together: parameters like PageRank and TCI, as well as the weight of the page and the reference weight of the old address will be passed to the new URL.

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

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

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

Внимание For the meaning of some notation (^, $, NC, etc.), see the reminder at the bottom of the page .

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

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

  Options + FollowSymLinks
 RewriteEngine On 

301 redirect from a domain without a 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 the domain with the 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 redirect from one static page to another

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

In this case, the new address must be specified completely with http and domain name.

In some cases, redirecting via RewriteRule is useful

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

301 redirect for page with GET parameters

For example, the address of the page looks like this: http: //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 more) of the GET parameters is not specified (or) or can have an arbitrary value (in our example this 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 addresses site.ru/index.php (without GET parameters) to the main site mirror.

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

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

Example: type site.ru/index.php?n=1 at 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

1 variant (simple address with GET parameter)

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

2 option (from page and GET parameter)

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

301 redirect for a particular file, not an entire folder

If you want to set up the redirection only for the address http://www.site.ru/dir/ , but at the same time the page http://www.site.ru/dir/index.php?IBLOCK_ID=1 opened 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 the same domain on the homepage of another domain

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

Each page of one domain is at the same address of another url

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

What about the domains in the RF zone?

For domains in the RF zone all the same rules apply, but only all Cyrillic symbols need to be replaced with an alternative code (it's in Latin). In particular, the zone itself. Pf is transformed into. 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 RF zone

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

Setting the redirect to the slash folders at the end / (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 (whole site)

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

Set up the redirect 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 on the slash (often in CMS systems 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 redirects without 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: //% 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 redirects to 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 redirects to www and without a slash at 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 redirects without www and without a slash on 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] 

Redirect from all domain files, except the bitrix administrator folder

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

Redirect all files in a folder to a specified file

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

Redirecting files from a specified folder except for 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 extension to php extension

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

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

The order of loading of types of an index file, which lies in the root of the directory, is specified.

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

Redirect from index page php 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 subdomain to primary second-level domain

  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 to one file in the root /unique-file.html. It is useful when you redesign the site and change the links.

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

If you want to create a CNC copy of a dynamic page, you can also implement it 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 error file 404 using .htaccess

Attention, it is important that the server response code for the 404 error is exactly 404. The path to the file is indicated with 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 adjust all redirects to the end page (without intermediate redirects, in one step), this improves their perception on the part of search engines and users.

If you want to configure a redirect only for some USER_AGENTs, and 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 want to set up a redirect for all search robots (see the list of their USER_AGENT'ov)

  RewriteCond% {HTTP_USER_AGENT}! (Accoona | ia_archiver | antabot | ask \ jeeves | baidu | dcpbot | eltaindexer | feedfetcher | gamespy | gigabot | googlebot | gsa-crawler | grub-client | gulper | slurp | mihalism | msnbot | worldindexer | ooyyo | Pagebull | scooter | w3c_validator | jigsaw | webalta | yahoofeedseeker | yahoo! \ Slurp | mmcrawler | yandexbot | yandeximages | yandexvideo | yandexmedia | yandexblogs | yandexaddurl | yandexfavicons | yandexdirect | yandexmetrika | yandexcatalog | yandexnews | yandeximageresizer) [NC]
 RewriteRule (. *) Http://no-search.site.ru/ [L, R = 301] 

A few simple examples

  Forwarding from www.site.ru/component/content/?view=featured to www.site.ru/
 RewriteCond% {QUERY_STRING} ^ view = featured $ [NC]
 RewriteRule ^ component / content /? $ /?  [R = 301, L] 
  Forwarding 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] 

We remove all GET parameters after the question mark (?)

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

The main page of site.ru is always full of its double 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 users to always see the main site name 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 ending .html (for those who have this suffix included), 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 with .html on without .html, i.e. From site.ru/article.html on 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 site.ru/blog?limitstart=0 on site.ru/blog

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

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

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

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

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

Redirect with a slash on without a slash in the end, i.e. From site.ru/article/ on site.ru/article

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

Another option is how to get rid of the trailing slash on the end

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

Option of getting rid of slash for pages with parameters, for example pages with pagination site.ru/categoriya?start=5/

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

First forgot to include SEO in global settings, and then included, as a result - in the index a lot of documents with /index.php in the address.

By the same principle, you can get rid of any nesting, for example redirect with site.ru/catalog on 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 bad agent's user agent
 Order Allow, Deny
 Allow from all
 Deny from env = bad_bot 

Or robots.txt gives, on the rest 404 (for the agent the 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 given similar different settings rules for 301 redirects.

Redirect with a script (sending headers)

Queries can also be redirected using scripts, sending the headers the necessary headers.

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

JavaScript redirect

That's where there is no limit to creativity and the opportunity to "get bogged down." Variants of JavaScript redirection are often implemented using the function setTimeout ('function', delay) .

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

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

On the button "Submit" you can hang any action, for example, open a new url in this window. By the way, such redirects are more frequent when arranging the Dorvei (DorWay) - the User's browser will be redirected to another page, and the search robot that does not understand JavaScript will index this page inaccessible to the User. On it the Dorveyschiki place the text, stuffed with the "necessary" keywords.

Just redirect to another page - insert after the <body> JavaScript code:

  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 no longer be possible to return to the page that performed the redirect, since The address of the page is deleted from history (which is often required).

If you need a delay in time, you can place location = "http://www.new.domain.ru"; In the form of a function and insert it into setTimeout ('function ()', delay_in_miles); .

How different search engines can 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 redirection as expected, while the user can show additional information why he is being moved to another address.

Since it may take several weeks or months to transfer a PR of an old site (page) to a new site (pages), do not destroy the old domain name, site or page until it 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

Implementing a redirect in nginx

  If ($ host = 'www.domain.com') {
 Rewrite ^ (. *) $ Http: //domain.com$1 permanent;
 } 

Memo for symbols and symbols used

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

Rules can be specified 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;
  • !! - a special symbol of negation;
  • . - a dot, replaces any symbol, but only one;
  • () - grouping;
  • \ - "screening" slash, the next character after it is considered ordinary, and not a special character.

Modifiers are used after normal, special characters or their groups and allow to expand the capabilities of templates for triggering rules.

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

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

  • NC - the NoCase flag, which turns off the case-control when the rule is triggered.
  • R - the flag of Redirect, produces a process to stop changing the URL and returns the result. The most common value is R = 301, but others are possible for temporary redirects (302, MOVED TEMPORARY).
  • L - the Last flag, stops the formation of the URL and the line is considered final.

Syntax for regular expressions

. - A dot replaces an arbitrary character.

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

[^ Abc] - a list of characters that are not within 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 consecutive characters from the specified set.

[^ Abc ] * - to the exact opposite.

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

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

$ Indicates the end of the line.

\ W is a letter, number, or underscore _.

\ D - replaces any digit.

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

[0-9] - replaces any digit.

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

[AZ] - any letter from A to Z in the UPPER register.

[A-zA-Z] - any letter from a to Z in any register.

[AZ] is the same.

All about redirection

Disclaimer: The materials for this article were taken from the vast expanses of the Internet (RUnet and BOURJUNET) and personal experience. Some points seem controversial and are now being tested in practice, so the information of the article will be supplemented and refined.
** In connection with the increasing number of messages 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 how to "unite" domain.ru and www.domain.ru. But other than how to do it, it would be useful to understand a little bit as well and why. This could 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 load, so it was necessary to put different "parts" of the Internet on separate machines.

For example, the Apache server was hosted on one computer, the mail server on the other, and the FTP server on another. Each of the computers responded to a different IP address, but to the same domain name. Inside this domain name, the computers differentiated by the service they provided (what was called then, the "machine name"). Thus, the names of servers on the Internet began with the "machine name" for the service it provides : www.domain.ru , mail.domain.ru , and ftp.domain.ru . ("Old-timers" of the Internet, probably remember also such archaic service, as gopher.domain.ru , in options IE it still remained).

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

Indeed, we often install several hundred domains, each with its own set of services (http, ftp, mail ...), on the same server. Therefore, at present, the www prefix is ​​"antiques" 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 print www before any name Domain.

This excursion into History, at least for me, seems interesting, but the only important thing of 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 should show the same page, unlike sub.domain.ru .

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

Creating an alias for www

If someone yells "Yuri!" In a crowded room, he's probably going to get my attention. If someone screams "Savilov!" In the same room, I'm going to respond to this as well. Because these two names, although completely different, generally point to the same person. Simplifying a little, we can say that "Yuri" is essentially a pseudonym (alias) for "Savile". If someone in that room approaches and calls me as "Oleg", I will probably look around, find "Oleg", and indicate him as what this someone is looking for (if this someone, not so pretty person, So I can lie :) . "Yuri" and "Oleg" are two different names that point to two different people, so I have to redirect the person's request to where they will respond according to his request. On 99.9% of all server configurations, domain.ru and www.domain.ru indicate exactly the same place on the disk. Everyone is just an alias for another. The use of redirection to indicate the same place on the disk has the same meaning as when someone calls to me "Yuri" and I answer: "No, you need Savilov, 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 should always create an entry for everyone, usually CNAME for everyone, or maybe CNAME for the primary and secondary entry. If the domain in question is on a static IP address, this is all we need to do to create a nickname. Most of us, however, share an IP address with other domains, so we need to move beyond the domain name system (DNS) level to the network server level to complete the alias configuration. Using Apache, for example, to create an entry point in the configuration file hpptd.conf

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

Naturally, you first need to associate a www-version with a www-version through a record in the DNS-server like:

For a top-level domain:

  IN A 192.xxx.xxx.xxx
 Www IN A 192.xxx.xxx.xxx 

For the subdomain info in the top-level domain:

  Info IN A 192.xxx.xxx.xxx
 Www.info CNAME info 

This is all that needs to be done. Surfer or Spider, going either to domain.com or to 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 access the domain with or without www . If for Surfer it is, in general, indifferent, then this can not 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 when That we know that they are pseudonyms. Google - the first search server, which began to apply complex double filters and the logic necessary to eventually "merge" domain.ru and www.domain.ru into a single object. Unfortunately, there is some little emphasis in that sentence on the word "ultimately". If you use the aliases of the Domain Name System (DNS) or ServerAlias, Google will eventually recognize domain.com and www.domain.com as a single object and will display only one site in the SERP. Unfortunately, this usually takes several months, and if you change the content of the site frequently, then because Spider indexes domain.ru/page.htm and www.domain.ru/page.htm at different times - it will receive different contents of the pages from www And without). Then these few months can stretch for a year or more.

Why is it necessary to "unite" domain.ru and www.domain.ru?

Many "serious" sites use only a domain with the www prefix (for example, most sites with PR 10. If you try to contact w3.org or adobe.com, you will see that your browser will be immediately redirected to the site with www . This Not just an alias, because the location of your navigation is actually changed to a new domain with www . I believe that Google normally treats this, not only because "serious" sites do this, but Google does it (if you do not believe it, type in Google.com in the browser.) Interestingly, most "serious" sites use Redirect 302. Only a few, like w3.org, return a 301 status code, apparently Google treats Redirect 301 and 302 equally.

The only reason Google believes that the site should be with the prefix www : you can compare Google search query "muzzle" site narod.ru - the difference will be palpable - without the prefix www, Google will include in the output of all subdomains. But, since there are precedents, when a site without www has PR higher than a site without www, then Google does not discriminate against everything in everything.

Therefore, instead of relying on aliases and filters of search engines (Spiders), someone came up with a brilliant idea to redirect one alias to another alias, which is essentially a redirect to itself. This is one of those things that is done basically BECAUSE the search engines exist and should, thus, help to avoid splitting Backlinks. However, there may be other reasons that require mandatory redirection: since technically http://mysite.ru/ and http://www.mysite.ru/ are VARIOUS sites, PHP sessions can not be transferred from one to another. This is another reason for rewriting the URL in the browser by redirecting to alias, especially if you have only one SSL certificate for the domain www.mysite.ru ...

Technical Redirects

Now let's try to answer the question why redirect recommendations often suggest mod_rewrite and RedirectMatch in the .htaccess file (requiring certain resources of the machine to perform them), and less often a simpler solution. Why can not we eliminate all the complex Regular Expressions and just add the line "Redirect 301 / http://www.domain.ru" (for the hpptd.conf version above) to our .htaccess ? Since domain.com and www.domain.ru are aliases and point to the same directory , any .htaccess directives in this directory will be applied to BOTH domains. A simple Forwarding, on some server platforms will lead in a "vicious circle" and, in the final analysis, collapse. Let's try a little to change the configuration of the server:

 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 put 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 do 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 DocumentRoot for the second object, we insert a simple redirect . We avoid the contradictions of trying to redirect a pseudonym without creating it. The drawback of this method is that it requires more work for the host administrator. It is necessary to define two blocks of records instead of one, but more importantly, it is necessary to support both blocks "in unison". The advantage is that each separate 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 applied for each individual page request, and then multiply all this by 200 to 500 other domains living on the server.

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

It remains to clarify the question: what is smarter than Redirect 301 or 302 ? As noted above, many "serious" sites quietly use Redirect 302 to "join" the domain with www and without and Google is just as calmly accepting it. But not the fact that the rest of the search engines will do the same. Still, RFC has not been canceled: the code "301" means that the page is permanently moved - "permanently moved" , the code "302" is a temporary move "moved temporary" , so the use of the code should depend on the purpose of moving the page. It should also be understood that many browsers, by receiving the "301 - moved permanently" response , can automatically reconfigure bookmarks to a new page. Similarly, not the fact that, the same Google, will timely transfer PR to the redirected 302 page, considering it to be "temporary", until it "goggles" both sites.

Redirect for different SE (Search Engines):

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

If on your site some of the links are installed on both www and part as on without-www, then you are probably interested in "combining" the weight of links to both versions of the site in terms of tIC / PR and reference ranking.

Redirect for Yandex

The fact is that Yandex unites links for sites that it considers as mirrors, and a redirect from site.ru on www.site.ru will exclude Yandex access to site.ru and, therefore, it will not be considered a mirror with all the ensuing consequences. For gluing Yandex, both site names should be accessible (they answered " 200 OK ") and had the same content.

Additionally, you need to define the main site mirror in the Host directive in the Robots.txt file, for example:

  User-agent: *
 
Disallow:

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

* It is more competent to render Host directives in a separate section only for Yandex robot (there is information that Google either ignores the section in which instructions are unclear to it, or it does not work out correctly);
** According to the robots.txt standard, at least one "Disallow:" directive must exist in each section of "User-agent:", so in the example there is an "empty" directive that does not prohibit anything. For your case, write down your own limitations, if any.

Redirect for Google

Google normally understands Redirect, at least this can be judged from a recent article 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
More about changing domain names
1/27/2006 09:27:00 AM
Posted by Vanessa Fox, Google Engineering

Recently, someone asked me about moving from one domain to another. He had read that Google recommends using a 301 redirect
To let go of Google. He wondered if Google would follow
The 301 to the new site, see that it is contained in the same content as the pages already indexed from the old site, and
Think it was duplicate content (and consequently not index it). He wondered if a 302 redirect would be a better option.

Redirected to exactly what he should do. 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. Googlebot will not see the new site as duplicate content, as moved
Content. And that's exactly what someone who is changing domains wants.

He also wondered how long it would take for the new site to show up in Google search results. He thought that a new site
Could take longer to index than new pages of an existing site. I told him that if he noticed that it took a while for a new
Site to be indexed, it was generally because it took Googlebot a while to learn about the new site. Googlebot learns about
New pages to crawl by Sitemaps. If Google already knows about a site,
It is on the same page.

I told him that by using a 301 to redirect Googlebot from the old domain to the new one and by submitting a Sitemap for
The new domain, Googlebot could much more quickly learn about the new domain. He
He could update his links.

The crawling and indexing processes are completely automated, so I could not tell him exactly when the domain would start
Displaying up in results. But letting Google know about the site (using a 301 redirect and a Sitemap) is an important first
Step in that process.

Here is the 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 was not sure if he should do it.

I told him that Redirect 301, that's 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 Guglebot that the move is permanent and that Google should start indexing the new domain. Googlebot will rassamtrivat new domain not as duplicated content, but as moved content.
. . .
Guglebot learns about new pages for indexing by links from other pages and from Sitemaps.
. . .

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

My URL is changed, so how can I get Google to index my new site?

While we can not manually change your URL in our search results, there are steps you can take to make sure of your transition
Is smooth.
First, you can redirect individuals to your new site. If your old URLs redirect to your new site using HTTP 301 (permanent)
Redirects, our crawler will discover the new URLs.
. . .
Google listings are based on our ability to find you from links on other sites. To preserve your rank, you'll want
To tell others who link to you of your change of address. One way to find a sampling of sites that link to yours is to
Perform a link search.

Here is the translation of this fragment: -

"While we can not manually change your URL in our search results, there are steps that you can take to make your transition smooth. At first, you can forward people to your new domain.If your old URLs are redirected to your new domain , Using HTTP 301, our spider will discover new URLs ";

"The Google issue lists are partly based on the ability to find the site from links from other sites." To keep your rating, you will want to tell those who refer to you about changing your address. "

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

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

If your site is appearing as two different listings in our search results, we suggest consolidating these listings so we can
More accurately determine your site's PageRank. The easiest way to do so is to redirect your http URL to your www URL using
A 301 redirect. This should solve the situation after our crawler discovers the change.
. . .
Please note: using a robots.txt file and our automatic URL removal system to remove just the http or www version of your
Site will not work. This will remove both versions for 180 days, and we can not manually add your site back to our index.

From which it follows that Google must combine PR sites in the redirect 301.

Note the note to the last fragment. You can not delete separately a non-www version or www-version from the Google index - you delete both versions immediately for 180 days.

Afterword

To check the gluing of the site by Yandex and the "merging" of the TIC, another method has been applied (it was 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 requesting the page, and if it's a Yandex robot - then both responses to the site name (with or without www) are given the answer "200 OK" and the page itself. For everyone else, the site is only available as www.info.data-com.ru, when you turn to info.data-com.ru, Redirect 301 is on www.info.data-com.ru. The same technology built a method of cloaking - when search engines show one content page, and users - another. Cloaking is punished by search engines excluding the site from their index and as a consequence - from the issuance.

To verify the association of Google PR by Redirect 301, this has been installed on this site since February 2006, and if RP www.info.data-com.ru and info.data-com.ru are aligned - this fact can be considered confirmed. (Links to www.info.data-com.ru and info.data-com.ru are about 50 to 50).

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