What is 301 redirect for and how to do it - Profit Hunter

What is 301 redirect for and how to do it - Profit Hunter

Sometimes you have to transfer a page or even the whole site to a different URL. To do this, use the 301st, permanent redirect. That is, if you follow the old links (internal or from external resources), people and search robots will get to the new URL address. 301 redirect is gluing the old and new URLs. At the same time, the old URL ceases to be indexed (this is the difference between the 301st and the 302nd, temporary redirect). This is necessary in the following cases.

1. If the site has duplicate pages. For example, in WordPress blogs, numerous duplicates often appear with addresses including "? Replytocom”, which are formed due to the insignificant (at first glance) "reply" button.

What is 301 redirect for and how to do it - Profit Hunter

The result is a mass of identical pages (equal to the number of comments), which are safely indexed by Google and entered into additional results of the output.

What is 301 redirect for and how to do it - Profit Hunter

The ban in Robots. Txt here, as a rule , does not help, and the site is pessimized.

To prevent this from happening, all addresses with "? replytocom" and similar "tails" are glued to the same page, but the duplicates themselves are deleted and the code that forms them is changed.

2. If you need to transfer pages to other categories, rename them, make them shorter, etc. In this case, pages with old addresses will be redirected to new urls, and the search robots will gradually glue them together. This is necessary, again, to avoid duplicates. In addition, redirects are needed if the old page is deleted in order to prevent the occurrence of broken links.

3. If it is required to make gluing the domain with www and without www. This needs to be done at the initial stage of development of the site so that it becomes available only at one URL. The fact is that the links are www. mysite. ru / * and mysite. ru / * differ, although they lead to the same pages. The result - unnecessary doubles. In addition, users will link to you with and without www, which will lead to the blurring of the reference mass, as the search engines will index only 1 type of links. To prevent such undesirable consequences, 301 redirects come to the rescue.

4. If you are moving your site from the old domain to the new . The reasons are different (bad “karma” of a domain name, a change of domain zone, for example, from. Ru to. RF, etc.). 301 redirect helps to save traffic and avoid broken links.

How to redirect 301

All manipulations take place in the .htaccess file, which is usually located in the root folder of the hosting. Before all redirects, you should specify the rule "RewriteEngine On" on a separate line.

To redirect pages from www to url without www there is the following code:

RewriteCond% {HTTP_HOST} ^ www. moj-sajt. ru $ [NC]

RewriteRule (. *) http: // moj- sajt / $ 1 [R = 301, L]

To set the inverse This redirection should be changed in the code value so that it becomes "^ moj-sajt. ru $ ”and" http: // www. moj-sajt. ru / ”.

If there are duplicates on the site with the category in the URL and without, and we want them to be available only at one address , let's write a similar rule:

RewriteRule ^ (. *) catalog / page. html $ http: // moj-sajt. en / page. html [R = 301, L]

In this case, the page that opened earlier at catalog / page. Html will be redirected to moj-sajt. Com / page. Html. And we will get a shorter URL.

Note that in the code, the following operators are used: "^" "$" - right and left constraints, respectively, "*" - any sequence and number of characters (starting from 0), "()" - this is constant prices. In the expression (. *), The dot denotes any single character. If you want to go deeper into understanding this syntax, then you need to refer to the documentation regarding the RewriteRule rules. You can do it easier: use the htaccess redirection generator - http: // www. webconfs. com / htaccess-redirect-generator. php.

If for some reason you changed the URL of the page (with a post, for example), you need to apply the following rule:

RewriteRule ^ (. *) Staraya-stranica. htm $ http: // mysite. ru / novaya-stranica. htm [R = 301, L]

Note that the 301th redirect can be delivered without setting the rules of RewriteCond and RewriteRule. For example, for transferring to another domain name, the following is prescribed:

Redirect 301 / http: // novoe-imya. ru

And for changing the url of the page they write:

Redirect 301 / staraya-stranica. html http: // my-site. ru / novaya-stranica. html

The only difference is that the code uses different rules, and both options are absolutely correct.

In what other cases is a redirect of 301 put?

  • if you need to remove the duplicate one at the address “mysite. Ru / index. Php” or “mysite. ru / main. html ”;
  • to change the“ tail ”(suffix) of pages, for example, from“ html ”to“ htm ”or from“ my-site. com / stranica. html / ”to“ mysite. ru / stranica.html ";
  • to move a group of pages from one category to another;
  • to merge two sites;
  • to redirect from a subdomain to a second level domain, etc.

How to check the correctness of the code?

Just go to the old address. If the browser redirects you to another URL, then the redirect works fine. If this does not happen, try clearing the cache and retry. When this does not help, look for the problem in the code itself.

Also, it is worth checking the server's response: it should produce "301".

Note that for the final transfer of pages to a new URL, it is the 301th redirect that is used, not the 302nd. In the second case, the redirection is temporary, and this signals the search engines to continue indexing old pages, while new URLs are ignored by search bots.

Do you like articles? Subscribe to the newsletter!


Related Articles