Htaccess redirect 301 from main to internal. Redirection from a domain from www to non-www and vice versa

As long as you have access to your site's directory on the server, such actions will not be difficult. We will assume that your site is running on Apache web server(like most sites), so let's talk a little about the technical part of implementing 301 redirects in Apache.

First a little theory

In website terms, a redirect is a method of automatic redirection end user from one URL to another. While on the technical side there are several ways to implement redirection, for tasks related to search engine optimization(SEO), we recommend using a permanent 301 HTTP redirect.

Unlike the default temporary 302 HTTP redirect, 301 indicates that the old, bound URL is no longer in use (whereas 302 indicates that the old one is temporarily not in use, but should reappear in the future).

The difference between 301 and 302 is especially important when search engines crawl your site. When a search engine crawler encounters a link to your site that is permanently set to redirect, the web server interacts with the 301 code status and then redirects the user to the new URL. Search engines not only accept the redirect to the new URL, but also transfer the page metrics value from the old URL to new page redirect (this is the second feature of such a process, which is very important for SEO optimization, which cannot be done using 302).

In all examples, the domain of this site is indicated, do not forget to replace it with the address of your site.

Comparison of 301 redirect and canonical rel=”canonical”

For example:

The point of the rel=canonical tag is to tell the search engine a single, whole address of the page's content. This is very useful when a site uses dynamic attribute variables for URLs. Dynamic URLs can help index multiple versions of the URL for each individual page, resulting in (and search engines just hate!)

While using a tag with the rel=canonical attribute is useful for a site page, it is not a reliable replacement for a 301 redirect. There are several reasons for this:

The rel=canonical tag is still considered search engines as half-hints, and not as specific directives for execution. Whereas a 301 redirect is perceived as a specific instruction. And finally, unlike the 301 redirect, which can be configured in one file for the entire site, the rel=canonical tag code will need to be inserted on each page.

All this tells us that using rel=canonical tags can be useful when redirecting blog pages (of course, if you have access to its code and insert the necessary modular tags into the pages). After all, using 301 may mean losing the original pages and their associated comments. social connections who have their own eigenvalues in SEO.

We recommend considering using the rel=canonical tag to minimize duplicate indexed content. Regardless standard reasons reassigning functions and values search index old URL to a new one, we recommend sticking to the proven method of 301 redirects.

How to make a 301 redirect in .htaccess?

On the web server Apache redirect 301 can be executed through script codes in one of two text configuration files: either .htaccess (for directories representing individual sites on the server), or httpd.conf (in root directory Apache installer). Usually the method with changing the configuration is used, so let’s look at it in more detail.

The first thing to do is open text file, which is called .htaccess . It can be found in your site directory on the Apache web server. Make sure to open it with a basic text editor such as Notepad on Windows computers.

After opening the file, before adding special code script, you need to do two things:

  • Turn on Apache module mod_rewrite.
  • Enable ReWriteEngine in the mod_rewrite module.

To do this, add these two lines of code:

Options +FollowSymLinks RewriteEngine on

Note that these lines only need to be added once in the file. Once they are added, you can begin adding the 301 redirect code to the script.

The following redirection script is used when moving data files, directory and domain name to the selected code. And, of course, don’t forget to put down the details of your site! This page contains only examples of such code!

301 redirect of one page to another URL

To perform a 301 redirect from one URL to another URL, paste next line code:

Redirect 301 /staraja.html http://www..html

On file. htaccess you can insert any number of redirect lines.

301 redirect of the entire directory and all files to another URL

If you redesigned the site's architecture and renamed a directory, you need to perform a 301 redirect to that entire directory. Do it like this:

RedirectMatch 301 ^/oldname/ http://www.site/newname/

Redirect 301 domain name to another URL

If you bought domain name with a good history, you will probably want to increase traffic to your site. Here you can use a 301 redirect of all traffic from that domain name to your current website. Use the following code as an example:

RedirectMatch 301 ^(.*)$ http://www.site

Make sure you create such a redirect in the file. htaccess of the source site from which you will redirect traffic, and not in the target site!

301 redirect to canonicalize to another URL

Since search engines index URLs, having different URLs that redirect to pages with the same content for indexing can affect PageRank. Of course, this situation is not the best for optimization! The bottom line is that if you want to attach PageRank to a single (canonical) URL, then to optimize search you need to pay attention to the content of the site pages.
When you study best examples canonicalization, you'll want to apply it to your site. This means that you should consider all possible alternatives to redirecting a URL to a canonical URL. Use the following example code for your site's home page:

RewriteCond %(HTTP_HOST) ^web-profy\.com RewriteRule ^(.*)$ http://www..(html|php|htm)\ HTTP/ RewriteRule ^(([^/]+/)*)( default|main|index)\.(html|php|htm)$ http://www.site/$1

The first block of two lines redirects URLs that omit the "www." prefixes to home page, for example, “www.xyz.com”..site/.

The second block of code redirects the URLs of the specified pages to the default addresses. This code ensures that any home page URL that has multiple spellings and deep links, such as default.htm or index.html, will be redirected to the canonical URL page, such as http://site

Documenting and testing work

# Redirect this entire domain, abc..*)$ http://www.site

Good documentation and commenting of processes will always help you (and those who will later work on the site) understand which code is responsible for what. Such information will be useful when searching for problems, when something is not working, to find out what part of the code needs to be fixed for the site to work smoothly.

Of course, it is necessary to test the work changes made. If you are using FTP to upload the corrected file. htaccess in the root directory of your site, it's time to check it. Enter the address in the browser Page URL which you redirected. It should instantly redirect to the specified URL.

Troubleshooting

If forwarding doesn't work as expected, then it's time to troubleshoot.

First of all, if the redirect is coded in your .htaccess file and it is written correctly, but it does not work, then check the installation status of the mod_rewrite extension in Apache. This module is usually installed by default, but if it is not there, then the .htaccess encoding above will not work. Also make sure you add two lines of code that enable mod_rewrite and ReWriteEngine to work.

In addition, note that the use of RewriteCond in the lines indicates that the input data does not match with established characteristics. In case you miss this point, URLs with capitals and lowercase letters may not work as expected. Note that the use of L in the code indicates that the engine file is perceived as the last line of code for the entire data entry confirmation process. If you have a conflicting code configuration in .htaccess, use the L code on the line designated as priority.

And finally, we note that frequent use 301 redirects are not good for the health of the entire site. While it is very useful, you must update your site's incoming links to the correct URLs. By ignoring your old incoming links and using multiple 301 redirects to click through them (301, then 301, then 301 again), you increase the time it takes to load the site, which is bad for site promotion. And if there are a lot of redirects, then the scanners may simply not reach the target site. If this happens, it harms your website's ranking in the search engine. Also, if you update your site's links, make sure that the sitemap.xml files are also updated with the new updated URLs.

Keep your site in check with 301 redirects, a standard good practice for white hat SEO. Make sure you help the scanners search engines reach the pages of your site and, at the same time, the earned PageRank is actively invested in promoting your site.

What happens is that a web page or entire site moves to a new URL permanently. In this case, it is necessary to register a 301 redirect. What is this? 301 redirect is a redirection of visitors and search robots from one URL to another.

A 301 redirect is usually written in the following cases:

  • when gluing website mirrors with www and without www;
  • when moving the site to new domain;
  • when moving a website to a new CMS;
  • when page URLs change;
  • when eliminating duplicate site pages.
  • Oddly enough, the last case is considered extremely rarely, although when eliminating duplicates, 301 redirects help like no other, unless, of course, you have the desire to tinker with the code of the CMS itself. And if you are engaged in search engine optimization of websites, then the question of the presence or absence of duplicates on website pages should definitely worry you.

    The 301 redirect is remarkable in that it transfers pr-pages and the site, and, accordingly, link juice to the new URL. But it does not transfer sanctions imposed on the old domain by search engines, which is doubly pleasant.

    So, we figured out what a 301 redirect is. Let's move on directly to how to make it.

    How to set up a 301 redirect

    To begin with, I will provide a list of operators regular expressions, which will be useful to you when setting up 301 redirects.

    • ^ — restriction on the left;
    • $—right constraint;
    • * - 0 or more characters;
    • + - 1 or more characters;
    • . - any symbol;
    • \ - shielding;
    • () - variable;
    • — range of values.

    There are various ways how to make a 301 redirect. Personally, I, like many other webmasters, configure it through the .htaccess file. Almost every popular CMS has this file, and it is located in the root of your site. You can download .htaccess using the FileZila client:

    The .htaccess file should be opened text editor, for example, Notepad++. On top of all redirect codes you should write the following line:

    RewriteEngine On

    Otherwise, redirects will not work.

    Many people mistakenly believe that they can do without 301 redirects. I can tell you that it is necessary when optimizing almost every website. For example, absolutely every website is initially available at two addresses - with www and without www. The trouble is that this leads to the erosion of the reference mass. I'll explain why. Other sites don't care which of the two spellings of your domain is the main one. If they link to you, they can include either an option with www or without www in the address. The reference mass will eventually be blurred, and search engines will index only one option in any case. Once upon a time, they generally perceived them as completely different sites.

    So that your site actively grows in positions and does not lose reference mass, you need to register a 301 redirect in the .htaccess file.

    From www to without www:

    RewriteCond %(HTTP_HOST) ^www.site.ru$ RewriteRule (.*) http://site.ru/$1

    From without www to www:

    RewriteCond %(HTTP_HOST) ^site.ru$ RewriteRule (.*) http://www.site.ru/$1

    If the above redirect codes do not work, then use the following entry to redirect from www to without www:

    RewriteCond %(HTTP_HOST) ^www\.(.*)$ RewriteRule ^(.*)$ http://%1/$1

    Remember that instead of site.ru you need to register the domain of your site. After these steps, save the .htaccess file and upload it to the server, replacing the old file with it.

    301 redirects often help me out when I’m struggling with duplicate pages on a site. And I do this almost every time I perform internal optimization another site. Personally, I have never met a CMS that did not generate duplicates. Although if we are talking about a small site, then there are exceptions. Have no idea what I'm talking about now? Then subscribe to, soon I will write a whole article about duplicates.

    It happens that a site page is accessible at once via two URLs of the form: http://site.ru/category/page.html and http://site.ru/page.html. IN in this case We are talking about doubles. For example, we want the page to be accessible only at the address of the second type. In this case, to get rid of the double, you need to write the following redirect:

    RewriteRule ^(.*)category/page.html$ http://site.ru/page.html

    Now imagine the situation that you have changed the address of a website page. For what? Yes, there can be a variety of reasons. For example, I recently did this to shorten the length of the URL. To do this you will need the following redirect:

    RewriteRule ^(.*)old-page.html$ http://site.ru/new-page.html

    If the above redirect code does not work, try the following:

    RewriteRule ^old-page.html$ http://site.ru/new-page.html

    Or this:

    RewriteRule ^old-page.html$ /new-page.html

    If you need to make a redirect from an old URL in Cyrillic to a new URL (either in Cyrillic or Latin), then the entry will look like:

    RewriteRule ^old-url.html /new-page.html

    As you understand, above we looked at redirects from one URL to another within the same site. For this we used mod_rewrite and RewriteRule directive. But you can get by with more simple option redirect entries using the Redirect directive:

    Redirect 301 /old-page.html http://site.ru/new-page.html

    Both redirect recording options are correct and convey link juice and other indicators. They differ only in that they use directives from different modules, and therefore have different syntax. When using the Redirect directive, first write old address pages without http://site.ru, and then new address fully.

    You may need a 301 redirect when moving your site from an old domain to a new one:

    RewriteEngine on RewriteCond %(HTTP_HOST) ^www\.old-domen\.ru$ RewriteRule ^(.*)$ http://new-domen.ru/$1 RewriteCond %(HTTP_HOST) ^old-domen\.ru$ RewriteRule ^(.*)$ http://new-domen.ru/$1

    A similar redirect using the Redirect directive:

    Redirect 301 / http://new-domain.ru

    You can set up a 301 URL redirect with a parameter to any other page of the site, for example, the main page:

    RewriteCond %(QUERY_STRING) ^cPath=373_703 RewriteRule ^index\.php$ http://site.ru/?

    The above example is from my practice, when I carried out internal optimization of a site on Joomla, which had huge amount various takes.

    Redirect from site.ru/index.php to site.ru (getting rid of the duplicate main page):

    RewriteCond %(THE_REQUEST) ^(3,9)\ /index\.php\ HTTP/ RewriteRule ^index\.php$ http://site.ru/

    As a result of the presence of a duplicate of the form site.ru/index.php on the main page of the site, similar duplicates may appear on the internal pages of the site, for example, site.ru/index.php/page.html is a duplicate of the page site.ru/page.html . In this case, all duplicates of this type can be eliminated at once using a redirect:

    RewriteRule ^index\.php/(.*)$ /$1

    If the main page of your site has a duplicate like site.ru/main.html, then you should enter the following redirect:

    RewriteEngine On RewriteCond %(THE_REQUEST) ^(3,9)\ /main\.html\ HTTP/ RewriteRule ^main\.html$ http://vash-sait.ru/

    or this option:

    Redirect 301 /main.html http://site.ru/

    Now imagine a situation where you want everything internal pages sites were accessible only at the address c.html at the end. Then you should enter the following redirect:

    RewriteCond %(REQUEST_URI) (.*/[^/.]+)($|\?) RewriteRule .* %1.html RewriteRule ^(.*)/$ /$1.html

    If, on the contrary, you want all internal pages of the site to have an address without .html at the end, that is, an address like site.ru/page, then you should register a redirect:

    RewriteCond %(REQUEST_URI) \.html$ RewriteRule ^(.*)\.html$ /$1

    If the task is to redirect from URLs with .htm at the end to addresses with .html at the end, then the following entry should be added to .htaccess:

    RewriteBase / RewriteRule ^(.*)\.htm$ $1.html

    You may need to redirect URLs without suffixes to URLs with a trailing slash. Then in the .htaccess file you should write the following:

    RewriteCond %(REQUEST_FILENAME) !-f RewriteCond %(REQUEST_URI) !\..+$ RewriteCond %(REQUEST_URI) !/$ RewriteRule (.*) http://www.site.ru/$1/

    If the task is completely opposite, and you need redirects from URLs with a slash at the end to addresses without the suffix, then add the following code:

    RewriteCond %(REQUEST_FILENAME) !-d RewriteCond %(REQUEST_URI) ^(.+)/$ RewriteRule ^(.+)/$ /$1

    To register a redirect from a URL with a parameter like http://site.ru/?page_id=111, add the following to .htaccess:

    RewriteCond %(QUERY_STRING) page_id=111 RewriteRule ^ http://site.ru/new-page/

    Also, just recently I came across a site for which a security certificate had been obtained, and it was necessary to register a redirect from http to https:

    RewriteCond %(HTTPS) off RewriteRule ^(.*)$ https://%(HTTP_HOST)%(REQUEST_URI)

    Now you know how to set up a 301 redirect. Of course, there are other cases where it is necessary, but I have listed the most popular ones. After any task related to website optimization, I recommend checking for errors.

    How to check 301 redirect

    301 redirects are very easy to check. All you need to do is enter the old page address in your browser. If a redirect occurs to a new URL, then most likely the redirect is registered correctly. In some cases, before checking a 301 redirect, you have to clear your browser cache. So if the redirection doesn't happen, you may have forgotten to do it.

    To finally make sure that the 301 redirect is configured correctly, I also recommend checking the server response using special services, for example, 2ip.ru, or better yet, use Netpeak program Spider, with which you can find out a lot of other information about the site. The server response should be “301” or “301 MovedPermanently -> 200 OK”.

    I hope you now understand how to properly configure a 301 redirect. Above I have given a sufficient number various examples. If one type of redirect record does not work, another will most likely work. Try it. I can’t say for sure what exactly which redirect entry will work depends on, since I’m not a programmer. Possibly from the CMS or server settings. The location of the redirect code in the .htaccess file also sometimes affects it. If the redirect does not work, try moving its code a little higher in the file. That's all for me. Good luck in website optimization!

    I collected everything possible ways make a 310 redirect using different methods. You can choose for yourself whatever your heart desires.

    Actually, let's understand the options for doing a redirect (forwarding) correctly.

    Simple 301 redirect in .htaccess

    If your server (or hosting) uses apache, redirection can be done via a file. htaccess. This method, in my opinion, is the simplest and most convenient of all I have seen. Important! Don't forget to enable mod_alias (to support Redirect, RedirectPermanent and RedirectMatch rules) and mod_rewrite modules in php.ini.

    1. Simple redirection from old pages to new ones: Redirect 301 /old/ http:// domain.com/new/ or Redirect permanent /old/ http:// domain.com/new/

    The only drawback is the need to make all changes manually, that is, each address is written directly by the programmer.

    2. 301 redirect in .htaccess for Russian-language links

    The only difference that took me a lot of time was the need to accurately save the document in UFT-8 format without BOM, which is a very important point. If you save in a different format, most likely your redirection will not work.

    Everything else is the same:

    3. Redirect using RedirectMatch

    A similar redirect method as described above, except that in this case you can specify a redirect if you have transferred the site from a php engine to aspx.

    RedirectMatch /(.*).php$ /$1.aspx

    4. Redirecting a domain from www to non-www
    Options +FollowSymLinks RewriteEngine On RewriteCond %(HTTP_HOST) ^www.(.*) RewriteRule ^(.*)$ http://%1/$1

    Another option in a simpler form:

    Options +FollowSymLinks RewriteEngine On RewriteCond %(HTTP_HOST) ^www.domain.com$ RewriteRule ^(.*)$ http://domain.com/$1

    5. Redirect requests without www to c-www Options +FollowSymLinks RewriteEngine On RewriteCond %(HTTP_HOST) ^domain.com$ RewriteRule ^(.*)$ https://domain.com/$1

    also solves a similar problem:

    RewriteEngine On RewriteCond %(HTTP_HOST) !^www.(.*) RewriteRule ^(.*)$ https://%1/$1

    6. Redirect links with a slash to without for the entire site RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) ![^\/]$ RewriteRule ^(.*)\/$ /$1 7. 301 redirect as in point 6, but vice versa RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) !\/$ RewriteRule ^(.*[^\/])$ /$1/ 8. Remove the slash at the end of the main link if it is without www RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) !\/$ RewriteCond %(HTTP_HOST) ^www\.(.*)$ RewriteRule ^(.*)$ http://%1/$1/ RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) ![^\/]$ RewriteCond %(HTTP_HOST) ^www\.(.*)$ RewriteRule ^(.*)$ http://%1/$1 RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) !\/$ RewriteCond %(HTTP_HOST) ^([^www].*)$ RewriteRule ^(.*)$ http://%1/$1/ 9. Remove the slash at the end of the main link if she is with www RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) !\/$ RewriteCond %(HTTP_HOST) ^www\.(.*)$ RewriteRule ^(.*)$ http://www.%1/$1/ RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) !\/$ RewriteCond %(HTTP_HOST) ^([^www].*)$ RewriteRule ^(.*)$ http://www.%1/$1/ RewriteCond %(REQUEST_URI) !\? RewriteCond %(REQUEST_URI) !\& RewriteCond %(REQUEST_URI) !\= RewriteCond %(REQUEST_URI) !\. RewriteCond %(REQUEST_URI) ![^\/]$ RewriteCond %(HTTP_HOST) ^([^www].*)$ RewriteRule ^(.*)$ http://www.%1/$1 10. Remove using the correct redirects /index.php (without GET) RewriteCond %(REQUEST_URI) /index.php RewriteCond %(QUERY_STRING) ^\z RewriteRule ^(.*)$ http://site.ru/? 11. 301 redirect for all addresses where there is index.php RewriteCond %(REQUEST_URI) /index.php RewriteRule ^(.*)$ http://site.ru/ 12. We redirect from a dynamic url to a static one

    option with GET

    RewriteCond %(QUERY_STRING) ^id=229 RewriteRule ^.*$ /supermodel/?

    option without GET

    RewriteCond %(REQUEST_URI) /test/ RewriteCond %(QUERY_STRING) ^id=229 RewriteRule ^.*$ /supermodel/?

    13. We redirect all pages of a domain to one url of another domain RewriteCond %(REQUEST_URI) (.*) RewriteRule ^(.*)$ http://site.ru/ 14. Redirects for SSL (redirection from http to https and vice versa)

    I wrote more about this, which, in the light, I propose to study.

    RewriteEngine On RewriteCond %(HTTPS) off RewriteRule (.*) https://%(HTTP_HOST)%(REQUEST_URI)

    Redirect using scripts

    Many people perform redirects using scripts. A small selection for variety.

    HTTP/1.1 301 Moved Permanently Location: https://new.com/new-k/new.htm PHP redirect

    15. ASP redirects

    17. ASP.NET redirect
    private void Page_Load(object sender, System.EventArgs e) ( Response.Status = “301 Moved Permanently”; Response.AddHeader(“Location”,“https://new.com”); ) 18. ColdFusion redirect
    19. JSP (Java) redirect
    20. CGI PERL
    $q = new CGI; print $q->redirect(“https://new.com/”); Ruby on Rails def old_action headers[“Status”] = “301 Moved Permanently” redirect_to “https://new.com/”

    There are also different recommendations from search engines, to implement 301 redirects. I recommend using Google recommendations, which provide very detailed recommendations on how to properly configure a 301 redirect.

    To place 301 redirects on Apache servers, just change the .htaccess file as described above. If you don’t understand how this works and the unlucky symbols in the description above are a big mystery to you, contact your hosting provider or write a question in the comments.

    How to make a 301 redirect (redirect) in WordPress using a plugin

    Many people use one of the most popular CMS WordPress. By various reasons we may need to do a redirect within the site. For example, this need arose for me when moving a website to a new domain.

    The most optimal solution It turned out to be a plugin for WordPress that solved all my problems. The downside of this plugin was only the manual creation of a list of links for redirection.

    Overall, the plugin for WordPress suits me quite well to this day.

    Let's look at some explanations related to the 310 redirect

    301 redirect or server error-301, is a response specified in the HTTP header and indicates that the old address has new way on an ongoing basis.

    ** 303 error indicates a temporary redirect path.

    A redirect is the redirection of a visitor from one site to another automatically. In short, the user ends up on a page completely different from the one whose address he typed in the search bar.

    The redirect is performed using a script, which performs the redirection. The trick being performed is useful for owners of those sites who want to purchase a more euphonious domain name.

    For example, changing the address of an online store leads to big losses, because some visitors will be lost. In this case, the script solves this problem: users also type the same address they know, but are redirected to a new page. A redirect will help in cases where several domain names are crossed. The site address can be entered by specifying www or simply quickly typing the main name, but it is with the help of the script that they end up on the same page.

    The redirect function is useful not only for maintaining good traffic to online stores. Promoting the site cost the owners a lot of effort, so it is logical that they strive to maintain their position. To create a more successful enterprise within an online store, you can glue several sites together. A redirect is used for this. This method The implementation of the redirect function, on the one hand, is very fast and reliable, on the other hand, search engines are suspicious of glued sites.

    Why is this happening?

    The fact is that search sites are machines that need to clearly index all resources. Glued Sites Make Efficiency Difficult fast indexing, which, in turn, reduces the smoothness of their work.

    The most dangerous in this context are sites that are indexed by robots, while directing the user to the resource from which the redirect occurs. But search engines do not always perceive linked sites negatively. We are talking about the 301 redirect function.

    Redirect 301 is a server-level service. It is an exception among other similar functions, since almost all search engines perceive it as friendly. In particular, the largest search engine Google resource, which is used most often, favorably redirects with a 301 redirect service to the desired page.

    The reason for the successful interaction of a 301 redirect with search resources is that it is fundamentally different from similar optimization services. It is designed in such a way that it functions clearly for a robot, which is a search engine. A redirect redirects to the desired page URL once and permanently.

    The impact of the 301 redirect on SEO promotion

    1. With a 301 redirect, addresses are merged. Search engine robots and users opening the site in a browser are automatically redirected to a new page with a new address.

    2. When merging addresses, the new url receives the full weight of the page, link mass and values ​​such as TIC.
    A similar redirect is best solution when moving a site to new system content management if you do not want to lose position and site ranking. My SEO blog uses several types of 301 redirects for redirection.

    That's all. I hope this article was helpful to you. If you know your own methods redirect settings, I’ll be glad to read about them in the comments.

    • Posted by Nikolay Korotkov
    • Date: November 29, 2013 at 09:23

    Hello again!

    Today I will tell you how to set up a 301 redirect. You may have already heard about 301 redirects more than once, but due to the lack of necessity, or perhaps because of the fear of doing something irreparable, you did not attach much importance to this, at first glance, complex abbreviation.

    Many articles have been written on the Internet about setting up 301 redirects. But looking through most of them, beginners may have their brains “exploded.” All these incomprehensible scripts and codes cause confusion for many. In fact, not everything is as complicated as it seems at first glance, and in this article, I will try to make it as clear as possible for everyone how to set up a 301 redirect.

    Now let's talk about everything in order. What is a 301 redirect? This is a common redirection of the visitor to another page or domain (read about what a domain is and how to register it). I’ll say right away that in this article I will not consider various scripts and complex codes, I’ll just show you a few simple redirection options that any webmaster will know more than enough.

    How to set up 301 redirect? Redirect.htaccess

    We will look at 301 redirects via .htaccess. Htaccess is service file configuration, which hides a lot of possibilities. Using it, you can deny access to individual directories, specify the encoding of pages, set protection for files and folders, make 301 redirects, etc.

    This is if in general outline, so that you understand its purpose, and it actually has much more possibilities than you can imagine. At its core, the file has no name and is written as an extension .htaccess with a mandatory dot at the beginning.

    If suddenly there is no this file, create it yourself. The easiest way to create a .htaccess file is to use FTP - FileZilla client(I wrote about him):

    To do this, go to the root directory of your blog, click in the empty field right button mouse/Create new file/Enter file name/.htaccess/OK. After this, the file will be available for editing. You can open it with any text editor, for example.

    But there are times when the ftp manager hides system files, including .htaccess. Therefore, if you do not find a file in the root directory, do not rush to create it. First, check for its presence in hidden files.

    To do this, go to the “Server” tab and check the box next to “Force to display hidden files" If in this case you do not find the .htaccess file, then create it.

    In what cases might a 301 redirect be needed?

    Let's start with the most important thing!

    In general, every resource on the Internet should have a 301 redirect. And the most common case when it is simply necessary is a redirection from a domain with a WWW prefix to a domain without it, or vice versa (www is also called a prefix). Now I’ll briefly explain why this happens so that everyone understands what we’re talking about.

    The fact is that for any resource on the network, one of the domain name options (with www or without www) must be the main one. Here is a simple example regarding my blog. Its main domain is http://site, A www.site- is his mirror. The main mirror of the blog is written in the robots.txt file for Yandex in the form of the Host directive.

    If you don’t have a robots.txt file, be sure to study it here, where I described everything in detail. Robots.txt mandatory must be present on every resource. Otherwise, the blog is doomed to failure! In addition to robots.txt, you need to specify the main mirror of the site in Yandex.Webmaster.

    But since it is already indicated in robots.txt, I recommend that you leave everything to the robot’s discretion. To do this, go to the “Indexing Settings/Main Mirror” tab and check the box next to the “At the discretion of the robot” item:

    In order to use this function, you must be registered with Yandex.Webmaster. I wrote about how to do this. Until recently, in the Google Webmaster Toolbar, there was a similar function where the main domain was indicated, but after entering latest changes developers, it disappeared.

    By specifying the main mirror of the site, your resource will be indexed correctly and will not create duplicates. Many beginners may not have even heard about the site’s mirror, which is a big omission that causes a lot of troubles in the future, which I will talk about below.

    After all the manipulations done with the mirror, search robots will already determine the main domain themselves. You can even test the redirection, everything should work fine. And we could stop there, but SEO is such a thing that you shouldn’t rely on luck and it’s better to play it safe once again by setting up a redirect.

    As for the www prefix, it should have died out a long time ago. But since there are still many resources on the Internet with the www prefix, it has a place. For search engines http://site And www.site these are two absolutely different addresses! And users often type the resource address with www into the browser line, so it’s too early for it to die out. Based on this, mirror settings are simply necessary!

    Consequences from incorrect setting mirrors

    In fact, there are not so many of them; I have highlighted only two main points. But their weight will make many beginners think seriously...

    1. Everything external links will be distributed between addresses with www and without www, depending on how you will be referred to third party resources or users. It turns out that your blog will be deprived of some links, which will negatively affect its promotion and traffic. But that's not the worst thing!

    2. Everyone knows perfectly well that search engines are constantly fighting for uniqueness! They hate duplicate content and severely punish sites with the same content. Now imagine a situation where your blog was initially indexed with the www prefix, but as the resource developed, a site without the www prefix gained more popularity (it is mentioned and linked to more often).

    Naturally, you don’t even know about it and continue to develop your resource. Over time it will grow unique content, natural links and comments. Things are going uphill, so how can you not be happy? But here's the problem! Search engines, observing a site without the www prefix, labeled it as not being unique! As a result, it will no longer be indexed, and over time it will completely drop out of the index!

    This will be scary and very offensive! And to avoid such sad situations, 301 redirects were invented.

    Redirection from a domain from www to non-www and vice versa

    How to set up a 301 redirect in this case? It's very simple. Open the .htaccess file and copy the code below into it.

    Redirect from www to non-www:

    RewriteEngine on RewriteCond %(HTTP_HOST) ^www.vashdomen.ru RewriteRule ^(.*)$ http://vashdomen.ru/$1

    and vice versa:

    RewriteEngine On RewriteCond %(HTTP_HOST) ^vashdomen.ru RewriteRule (.*) http://www.vashdomen.ru/$1

    Please note that instead of yourdomain, in both cases you need to specify the domain name of your blog!

    Save the .htaccess file and move it back to the root directory, replacing the old file. Now, no matter how the user enters the address of your blog into the browser line (at least website, at least www.site, at least http://www.site), it will in any case be redirected to home page.

    Moving from one domain to another

    A similar 301 redirect scheme given above can be used when moving from one domain to another. But still, for clarity, I will give an example. You are moving from a domain sait-1.ru per domain sait-2.com. In this case, write the following in the .htaccess file:

    RewriteEngine On RewriteCond %(HTTP_HOST) sait-1.ru RewriteRule (.*) http://sait-2.com/$1
    Redirect when changing CNC links

    Sometimes it becomes necessary to change CNC links (read about them). Perhaps you have decided to change an entire section (category) on your blog. Or they simply didn’t know anything about CNC, but the blog is already being written and filled with content. In this case, a 301 redirect will come in handy. Moreover, in this situation, all page indicators (previously placed links, weight, PR) will remain at the same level, i.e. will not lose their significance, authority and position!

    Let me give you a simple example. You have decided to change the page title. The old page name was: obo-mne.html, new name ob-avtore.html. In this case, you will need to write the following construction in the .htaccess file:

    Redirect for affiliate programs

    Let's say you decide to redirect the user to a page with affiliate program. The affiliate code looks like this: http://www.partnerka.ru/?ref=35677. Having seen such a link, most users will not click on it, knowing in advance about your intentions.

    In order for the link to take on an attractive appearance, you need to create separate page, for example dlya-partnerov.html, from which the user will be redirected to the page with the affiliate program. Now all that remains is to place a link to the page vashdomen.ru/dlya-partnerov.html, and write the following in the .htaccess file:

    redirect 301 dlya-partnerov.html http://www.partnerka.ru/?ref=35677

    But personally, for such purposes, I prefer to use the simple script that I talked about (method No. 3).

    That's probably all. Now you know how to set up a 301 redirect and you shouldn't have any problems with it. I showed you the simplest version of a 301 redirect via .htaccess, which every webmaster should know about. Well, the last thing I want to say is always check the redirects for functionality. That's all, bye everyone!

    How do you like the article? Do you use 301 redirects? Perhaps you know of easier redirection methods? I'm waiting for your comments!

    Did you like the article? Share with your friends!

    A book for every commentator!

    The book includes detailed description the most effective methods promoting your resource!


    60 comments

  • 29 November 2013 15:20

    I read it and didn’t understand a thing... For people like me, there is only one thing left to do, find a person, tell him to set up a 301 redirect and trust that he did everything =))))

    So it still didn’t work out well to explain...

    No, no, everything is very sensibly and clearly written - thank you!

    It’s just that many are lazy to follow the recommendations, many do not attach serious importance to their projects, and so they do not bring them to fruition. And on some issues there are conflicting recommendations. So, in robots.txt it is recommended to place the indication about the main mirror of the site at the beginning of the file, then comes empty string, then the rest of the data. I don’t dare judge how true this is, but the author of this recommendation actively insists on this order of filling out robots.txt, and where can beginners figure it out?

    Why do they get lazy right away? If your brain is sharpened to sit and pick out codes, then others do not have this gift and in order not to fail everything, they either don’t do it, or, as in my case, I turn to a specialist and he will set everything up for me... So. ..

    Namesake, we’re not talking about you!

    You are a specialist in YOUR business, to whom it is strongly recommended to delegate routine technical work.

    So, be creative, and don’t bother yourself with what distracts you from your main task.

    The point is that there are many people who make websites “what would be”, or “I’ll finish it later”. So I haven’t fully implemented even what I already know: my main work and other things are getting in the way... Yes, there will always be plenty of reasons not to do it. But this is the biggest mistake when a person has not done one thing, but is already looking for another. Accumulated and not applied knowledge is useless, alas.

  • November 30, 2013 11:12
  • 30 November 2013 16:02
  • Anna November 30, 2013 19:59

    I finally understood what a redirect is, especially with examples, everything is well explained. And in general, I appreciate Nikolai’s articles because they write in clear language what it is, why it is needed and how to bring it to life.

    On the topic of this article, there is only one question left: if we enter a domain name with or without www and get to the main page of the site, then the redirect is configured and there is no need to enter anything, or in any case you need to write the code specified in the article in the .htaccess file, if it is there are we not watching?

    If the redirection works correctly, then you don’t have to write anything in the .htaccess file. But personally, just in case, I still registered a redirect in .htaccess, it won’t make it any worse.

    It turns out that whoever does not write from www still ends up on the site?

    I just thought that this was the functionality of the CMS itself, and never thought about it...

    Alexander

    Right! For these purposes, a 301 redirect is configured. This is not included in the CMS itself.

    How will I know if this redirection stops working? Everything is great now!

  • Elena February 1, 2014 22:55

    I wrote something in a comment, but didn’t go - it’s a pity. that one was longer.

    In general, I read and read and nothing came to me.

    Tell me how to redirect each page?

    After all, you can later, when it is indexed on a new site, remove the articles from the old one and remove them on the new site (each link has a redirect). And what will happen in general - the search engines will be shocked and will delete everything, or will index it again.

    Show an example on your website of one page on another - how to put it correctly, otherwise I did (I found the information on the internet) and nothing worked out.

    Redirect 301 /old page.html site.com/new page.html

    Redirect permanent /old page.html site.com/new page.com/

    In your case, a redirect for changing the CNC links, described by me in one of the paragraphs of this article, will be suitable. Moreover, as you correctly noted, after re-indexing the pages, it will be possible to remove articles from the old site and remove redirects. Search engines will react normally to changes of this kind. They will perceive this as a normal redirection.

    And at your request, I’m showing a simple example on my blog. Let's say I want to redirect visitors from the All Blog Articles page to the My Courses page. In this case, I write the construction as follows:

    redirect 301 vse-stati-bloga.html site/moi-kursy

  • Pavel March 25, 2014 11:37 pm

    And in my .htacces file only this code is written:

    # BEGIN WordPress

    RewriteEngine On

    RewriteRule. /index.php [L]

    what should I change? Is the file compiled incorrectly?

  • April 29, 2014 10:39

    Nikolay, good afternoon!

    I’m translating a 15-page business card to a self-written website on WP. The site is 3 months old, but there are already 10 users, a small thematic traffic, so I don’t want to destroy everything. A new website on wp is installed and configured, the website pages hang out on the server in parallel, but are not visible to the user.

    As far as I understand, a redirect for CNC is suitable for me, after setting it up it will be possible to either transfer the old content, or write a similar new one and the handwritten page can be removed from the server or what?

    Please tell me, testing a redirect is just going to the old address and seeing that everything opens correctly on a new page? Does that mean search engines will see it in the same light?

    Thank you very much in advance!

    Hello, Irina!

    You understand everything correctly... A redirect for CNC is suitable in your case. If the old pages were indexed by search engines, then transferring the old content to a new domain without a redirect will be considered a duplicate; it is better to use a redirect. And testing it is very simple. If when entering an address old page the browser redirects you to a new domain, the redirect works as it should and search engines will soon re-index the content. Then it will be possible to delete old dangling pages from the server without problems.

    Nikolay, thank you for the quick response!

    Only the old domain remains, I just switch from the handwritten version to wp and that’s it.

    I checked the server response code - there was a surprise waiting for me

    Instead of 200OK - 301 redirects. Opened htaccess. and what do you think? There seems to be a standard set:

    # BEGIN WordPress

    RewriteEngine On

    RewriteRule ^index\.php$ - [L]

    RewriteCond %(REQUEST_FILENAME) !-f

    RewriteCond %(REQUEST_FILENAME) !-d

    RewriteRule. /index.php [L]

    Can you tell me how to set up a redirect via 301 without paying attention to the server’s response?

    Thanks in advance!

    Status code 301 means that the requested page has been permanently moved to a new location. Standard Rule in the .htaccess file is precisely what is responsible for the redirect. So that the server’s response in this case is shown correctly, in the future the redirect can be removed and old pages can be deleted from the server.

    Yes, I understood, but it’s not entirely clear where this redirect came from, because I didn’t set it and how it was implemented if htaccess. it’s not there... Apparently, it was somehow automatically added by the system after I created index page in wp, and deleted the old handwritten one. Now I will register a redirect to the remaining pages in htacess manually.

    Thank you for the article.

    Nikolay, I also wanted to ask a question based on the speed of response, but I don’t know where. For some reason, site search doesn’t work for me, but I don’t want to spam the topic.

    Sorry, I forgot to specify where the line goes

    redirect 301 obo-mne.html yourdomain/ob-avtore

    put in already finished design

    # BEGIN WordPress

    RewriteEngine On

    RewriteRule ^index\.php$ - [L]

    RewriteCond %(REQUEST_FILENAME) !-f

    RewriteCond %(REQUEST_FILENAME) !-d

    RewriteRule. /index.php [L]

  • Irina April 29, 2014 20:26
  • Irina 19 May 2014 22:34

    Nikolay good evening!

    We need your help again!

    In the last update, Yandex chose site.ru with www as the main mirror (and since it was without www, tits was reset to zero!)

    All hands didn’t get around to it, but now it will have to... The problem is that I recently transferred a handwritten site to WordPress and those pages on which there were links, I have already redirected with 301 redirects to new addresses, my htaccess now looks like this:

    redirect 301 /file.html site.ru/new category name/

    # BEGIN WordPress

    RewriteEngine On

    RewriteRule ^index\.php$ - [L]

    RewriteCond %(REQUEST_FILENAME) !-f

    RewriteCond %(REQUEST_FILENAME) !-d

    RewriteRule. /index.php [L]

    I tried to attach the redirection both before the line # END WordPress and at the very top of the file

    - gives a redirection error, writes that it will never end...

  • About 301 redirects have probably already been said and retold many times in blogs, forums, etc. But, as it turns out, not everyone gets this information on time (here I seem to be hinting at myself :). For more than 3 years on the Internet, I heard about 301 redirects many times, sometimes I was even going to “try” it, but let’s face the facts - I never did. But in vain! It all started quite prosaically - I have one website that is constantly struggling in terms of indexing by search engines. It seems like there are links there, and the content is normal, but it still doesn’t want to work stably. I had almost run out of options for this behavior, but then I remembered the basics SEO basics and promotion in general - 301 redirects.

    A quick poll on Twitter showed that in reality I’m almost the only one who misses this important point. The majority unanimously answered that it is mandatory to install 301 redirects immediately when creating a website. Moreover, I was recently asked to develop a website to add this same 301 redirect for them, since the request came from a promoting company. Taking into account all these points and “hints of fate”, I realized that there would be a 301 redirect!

    Why do you need a 301 redirect at all? - you ask - there are several situations in which it can be used:

  • To merge a domain with www and without www. In this case, the indicators and link weight will be combined, otherwise it sometimes happens that for a domain with and without www they may differ.
  • When changing a domain from an old to a new one, a 301 redirect will again allow you to save the indicators and the link (I don’t know about the TCI, but PR for sure).
  • When moving a page on a website, so that search engines and visitors end up on a new page instead of the old broken one.
  • If there are PR domains with links that for some reason you don’t use, perhaps you just don’t have time, then theoretically you can use 301 redirects to your other sites. Although this method is an additional possibility or something, the main ones are still the first three.
  • In general, a 301 redirect is needed both for users and for search robots - it allows both to orient that there is a new site, domain, page and redirects them there without any questions. In addition, a 301 redirect will combine site indicators and allow you not to lose positions in search engines.

    How to make a 301 redirect

    There are many solutions to this problem, it all depends on what technical means your hosting has. There are also many articles on 301 redirects on the Internet that you can read; I will give only a few, in my opinion, the most typical situations.

    Simple redirect

    This is done in the .htaccess or httpd.conf file for Apache. The simplest version of a simple 301 redirect to redirect to a new domain looks like this:

    Redirect 301 /site1/page1.htm http://www.site2.com/page2.htm

    Here are a couple more examples of a simple 301 redirect:

    Redirect permanent /test http://www.test.com/ Redirect permanent / http://enter.test.com/

    Here, when a user or robot enters the test directory, it will be redirected to www.test.com, all others will go to enter.test.com. For this 301 redirect, the mod_alias modules must be enabled on the hosting (to support Redirect, RedirectPermanent and RedirectMatch).

    301 redirect using mod_rewrite in .htaccess

    You've come across the mod_rewrite module quite often without even realizing it. In particular, we are talking about permanent links (permalinks) like useful tool V seo optimization wordpress. If you set up these same links in the admin panel and then go to the .htaccess file, you will find there a whole series of rules for redirecting through the RewriteRule directive. In addition, you need to check that the FollowSymLinks option is enabled.

    Redirecting a domain from www to non-www

    Options +FollowSymLinks RewriteEngine On RewriteCond %(HTTP_HOST) ^www.domain\.com$ RewriteRule ^(.*)$ http://domain.com/$1

    Redirect requests without-www to a domain with the www prefix

    Options +FollowSymLinks RewriteEngine On RewriteCond %(HTTP_HOST) !^www\.(.*) RewriteRule ^(.*)$ http://www.%1/$1

    In general, the use of www in the name of a site is itself outdated, but sometimes still occurs. If you are creating a new website, then of course you immediately indicate everywhere without www, but if you received a “finished product”, then you need to look at how the domain is displayed in Google results and Yandex - you leave such a redirect so as not to change anything radically.

    301 redirect of an old domain to a new one:

    RewriteEngine on RewriteBase / RewriteRule ^rewrite\.htm$ rewrite.html

    To replace all .htm files with .html files:

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

    To do this, for example, in PHP we use:

    This code is best inserted at the beginning PHP script so that nothing is output before it (echo or print). Thanks for the explanation to the author of this article, where you will also find information about installing a redirect to ASP, ColdFusion, etc., it’s just that with PHP the most popular option.

    Notes on 301 redirects

    Finally, a couple of points about 301 redirects that you need to remember and which I have encountered in one way or another:

    • Firstly, the sequence of redirect rules is taken into account. So if some rule is not followed, check if there are other redirects and if they are interfering.
    • Secondly, remembering the syntax and all the features of a 301 redirect is quite difficult, so sometimes logic and trying out some options helps a lot :) Although, of course, it is better to read the official documentation.
    • Thirdly, after implementing a 301 redirect, be sure to check the rules for functionality, go not only to the main page, but also go through the sections and pages of the site so that there are no problems with this later.

    If you have anything to add regarding 301 redirects, write :)

    P.S. Guard. Nowadays, every day there are many interesting internet projects. If you want to know everything about startups, then read the new exciting StartupWay blog.
    After the audit and optimization of the web project, comprehensive promotion of sites in search engines follows. Google systems and Yandex.