301 redirect in htaccess to another domain. HttpFox add-on for Firefox. Deny access from different IPs

Hello. It is quite possible that many of you already know about how to make a 301 redirect and how to configure it. But, as you know, my blog is my cheat sheet. Therefore, I believe that it will not be superfluous to write this post and, when necessary, you and I can ask for this information. Because it’s impossible to keep all these codes in your head, and it’s not necessary.

What is a 301 redirect and where can it come in handy?

This is something automatic redirection from one page to another, and with this redirect everything is transferred: page weight, TCI, PR, weight of incoming link mass, etc. That is, the old page is glued to the new one. A user who lands on the old page will automatically be transferred to the new one.

Where a 301 redirect can come in handy:

  • Have you decided to move to new domain. In this case, this can be done most painlessly using this type of redirection.
  • Did you have a website on some engine (for example, Joomla). The pages had the address site.ru/?id=1245. On WordPress, the same page looks like this: site.ru/rubrika/post.html. And, in order not to lose the performance of old pages, you need to make a 301 redirect to new pages.
  • Your website is accessible at 2 addresses, for example with www and without www. In this case, you also need to register a 301 redirect.
  • Content on old page needs to be transferred to new page. Or, for example, 3 small articles need to be combined into one large one. IN similar cases It’s also better to move it to 1 of the 3 URLs, and add a 301 redirect to the remaining two.

How to make a 301 redirect in htaccess

I prefer to do 301 redirects using htaccess file. Moreover, it exists not only on WordPress sites, therefore this method redirects - universal. The .htaccess file is located in the root of the site:

Editable this file by using text editor, it is better to use .

Attention! For beginners: if you are not entirely sure of your actions, be sure to make a backup copy of it before making changes to the htaccess file.

From WWW to without WWW (and vice versa)

The site must be accessible in only one way: either with WWW or without WWW. It is wrong if the site is “given away” in two ways. You only need to leave one.

If the site is young, it is better to choose without WWW, but if it is old, then it is better to choose the option that is indexed. It happens that in Yandex it is indexed with WWW, but in Google without WWW, in this case, select the option that “came up” in the highest priority search engine for you.

Understand that www.site.ru and site.ru are for search engines These are 2 different sites, so only 1 option should be available! With WordPress there are usually no problems with this, only 1 option is available by default, but on other engines you can often see that the site is available in both ways.

301 redirect from www to without www:

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

301 redirect from a site without www to with www:

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

From one page to another using htaccess

If you need to set up a 301 redirect from one page to another, you need to write this code in the htaccess file:

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

From index.php (index.html) to the root (main page)

On some engines you may find that the main page of the site is also available at: site.ru/index.php. It is not right. This page needs to provide a 301 redirect to home page, here's how to do it (don't forget to rewrite "site" to your domain).

If from index.php:

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

If with index.html:

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

From http to https:

If you need to register a 301 redirect from http to secure httpS protocol, use this code:

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

From one category (directory/folder) to another

There is a need when all pages of a certain category need to be redirected to another address, here is the code:

RedirectMatch 301 ^/category-old/(.*) /category-new/$1

From one domain to another

If you suddenly moved to a new domain, then you need to write this in the htaccess of the old domain:

RewriteEngine On RewriteCond %(HTTP_HOST) old-domen.ru RewriteRule (.*) http://new-domen.ru/$1

Also, be sure to specify host in the directive new address site.

301 redirects on WordPress using the Simple 301 Redirects plugin

If a client’s site is on WordPress and he himself wants to add 301 redirects in some places, I install the Simple 301 Redirects plugin. Yes, and it is good for beginners. The plugin is very easy to use and everything is done in the familiar WordPress admin area, so this method may seem easier to some.

Conclusion

After you register a 301 redirect, be sure to check the site for functionality. One small mistake can make your entire site unavailable. Be sure to do it before changing the htaccess file backups, or use CTRL+Z to return the file to its original position.

Also: keep in mind that the weight of 301 redirects is not transferred immediately. Yes, gluing happens faster in Google, but in Yandex it all happens slowly. You usually need to wait from 1 to 3 months.

Don't forget to add this page Bookmark it, it will come in handy. And of course, thanks for the retweets.

Before moving directly to practice, it must be said that there are two types of redirect. I think you understand that this is a redirection from one address to another. It is necessary when, when moving to one address, you need to send a person to another place.

The redirect can be temporary or permanent. Permanent (301st) transfers reference weight, temporary (302nd) does not. Today we will talk specifically about a permanent redirect, since its use is considered optimal.

Setting up 301 redirects and practical cases

Usually a redirect is written in the .htaccess file, but this is not the only way to create it. Later in the article I will also show an option on how to make a redirect with using php.

But first we'll talk about how it's done through .htaccess. In general, using a redirect you can get rid of many problems. One of them is possible duplicate pages on the site, since it is essentially available at two addresses: www.site.ru, site.ru.

And for a search engine these are different sites. Thus, each page of your sites has at least 2 addresses where it can be accessed. This is if you removed other takes. To prevent such things, they usually redirect from www to the non-www option, since it is shorter and more optimal. Thus, domain gluing occurs. The main mirror will be site.ru. If the user types www.site.ru himself, he will be automatically redirected to the option without www. This will allow you to avoid duplicates, since now search engine will understand that the domains are related. And here is the code itself:

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

RewriteCond %(HTTP_HOST)^www. site\. com$ [NC]

RewriteRule ^(.*)$http://site.com/$1

Naturally, here you need to replace the site address with yours. It's worth noting that this is not the only way to avoid problems with www. Another option is to specify the Host command in the robots.txt file: domain without www. Another way is to set up the main mirror in Yandex.Webmaster. This is generally the simplest option for Yandex.

Great, but what if you need to redirect from one domain to another? In this case, use this code:

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

RewriteCond % (HTTP_HOST) ! ^www\. site\. com

RewriteRule ^(.*)$http://www.site.com/$1

Where in the first line you need to enter the name of the domain from which the redirection should occur, and in the second line enter the address to which the redirection actually occurs.

Actually, to make things as simple as possible, I can recommend this service to you - http://www.webconfs.com/htaccess-redirect-generator.php. This is the so-called redirect generator.

Redirecting with php

I also want to show how to do this using php.

header ( "HTTP/1.1 301 Moved Permanently") ;

header ( "Location: http://www.site.ru/page.htm") ;

exit();

This code redirects the user from the page where it is inserted to site.ru, where there will be a request for the page page.html and its display if it is found.

Once I took a couple of lessons on the basics of PHP and there was another example of how to use a redirect. Let's say you have information on your website that you are ready to show to the user only after registration or only after he enters his e-mail. In general, it doesn’t matter what kind of condition you set.

The point is that if the condition is met (the correct data is entered, the correct e-mail is entered, etc.), then you redirect him to a closed page and the person gets access to the information. If he entered the data incorrectly, then return him back to the input page. In general, here is a simple example of using redirections in php.

When should you use a 301 redirect?

A permanent redirect should be used in all cases when the current address is out of date and the information has been moved to a new location. For example, you moved articles from one section to another or even from one site to another. This is where a redirect will help you, which will let search robots understand that the information has moved to a new location.

A redirect is also used when there are several domains and one main one. Usually, all additional domains are redirected to the main one. This is especially true for large companies. For example, you have a large company with a beautiful name. But there are times when people make typos. Or maybe your competitors just want to take away a domain name that is similar in spelling to yours in order to conduct fraudulent activities on your behalf!

Of course, you can’t completely protect yourself from such people, but many people buy 3-10 domains that are close in name and domain zones to the main address, and then make them mirrors. That is, when you go to any of these addresses, you will be redirected to the main address.

Possible mistakes

Now let's look at the most common mistakes when using redirection. The first is any multi-stage redirect. This is when, when going to an address, a redirection is first made to one place, and from there to another, etc. This is basically strange and suspicious, because it is usually used for all sorts of gray operations. And you, an ordinary webmaster, will hardly ever need it. In addition, this kind of trick greatly reduces the loading speed of the final page.

The second error is the so-called broken redirect, when the redirection occurs to a page that does not exist (error 404). Or any other error like 4xx or 5xx is given. The only correct server answer is 200.

Many have heard it more than once. But due to the lack of need for it, and also because of the fear of doing something wrong, they did not attach importance to such a procedure. This article will discuss in detail how to set up a 301 redirect.

You can find dozens of articles and information materials about setting it up. But after viewing many of them, novice computer users and webmasters may have difficulty understanding the basics of configuration. Numerous codes and scripts provided in online sources of information are mostly misunderstood. In reality, everything is much simpler, and this article will serve as a good guide on how to set up a 301 redirect.

What is a 301 redirect?

Let's figure it out in order. First of all, let's define a 301 redirect. It is understood as the simplest redirection of a guest of an Internet resource (site) to another page on the Internet or a domain name. Let’s make a reservation right away that this article will not discuss any tricky scripts or overly complex code, but will show several simple redirection options, which are sufficient basic knowledge on this topic for any webmaster.

Tool for - .htaccess file

Let's consider such a convenient tool as .htaccess. Doing a 301 redirect through it is very convenient and simple. Htaccess is essentially a special service configuration file that contains a lot of functionality and many possibilities. Using it, you can set a ban on access to any directories on the site server, specify the encoding of pages, set protection for folders and files, perform 301 redirects and some other actions.

This is a description of the capabilities of this file in general terms. In fact, its functionality is even more advanced. The file has no name and is written as .htaccess. The period at the very beginning is mandatory.

Procedure for creating a .htaccess file

If this file is not in the root folder of the site, you can very easily create it yourself. One of the fastest ways to create is to use an FTP client. The most convenient of them is FileZilla.

To create, you need to go to the root directory of your Internet project - right-click on the computer mouse in an empty space - select "Create a new file" - enter the name .htaccess - click OK. After these steps, the created file can be edited. It can be opened with any text editor.

But it also happens when FTP clients hide some system files, including .htaccess. Therefore, if this file was not found in the root folder, there is no need to rush to create it. First of all, check for its presence in hidden files. To do this, go to the “Server” tab and check the box to force the display of hidden files.

So, if the .htaccess file has been created or found, it will be easy to create a 301 redirect using it. However, first of all you need to decide what it is needed for.

The most popular case when a 301 redirect can be useful

According to the recommendations of leading webmasters, every resource on the Internet should have 301 redirects. And the most popular case when it is very necessary is redirection from a domain that has a prefix with www to a domain without it and vice versa.

This is explained by the fact that for any website on the Internet, only one type of domain name with or without www can be the main one. Another option will be a mirror of the site. The main mirror of an Internet resource is signed in the robots.txt file using the Host directive. In addition to this file, the main domain option should be specified in Yandex.Webmaster. Many search engines are very accepting of tools such as 301 redirects. Yandex is no exception to this.

When you specify the main mirror of an Internet resource, your site will be indexed correctly and will not create unnecessary duplicates. Many newbie webmasters have never heard of mirror sites, which is a very big mistake that will cause major troubles in the future.

The value of setting up site mirrors

After all the actions taken with the mirror, search engines will determine the main domain automatically. You can even easily check the redirect, everything will work fine. We could stop at these steps, but the best way to optimize an Internet resource for SEO would be to additionally configure the redirect.

The www prefix itself in the designation should have gone into oblivion a long time ago. But nevertheless, there are still many resources on the Internet with www in the name. For search engines, domains with and without www are two different addresses. And many users, when searching for a specific Internet project, type www into the address bar. Therefore, this prefix still exists. Based on this fact, adjusting the mirrors is a necessary action. And the guide on how to set up a 301 redirect is also a must-read so that your Internet project is successfully indexed in search engines.

Consequences of incorrect mirror settings

In order to appreciate the full significance of 301 redirects and the correct configuration of mirrors, we will highlight the detrimental consequences of their incorrect operation. There are not many of them, only two. But the weight of each point is very great.

They will be divided between domains with and without www, directly depending on how other users and sites will link to your resource. This means that your Internet project will lose some of its links, which will negatively affect its traffic and place in search results. However, this is not the main problem.

Many webmasters understand that search engines are constantly fighting for the uniqueness of the content of Internet resources. They have a very negative attitude towards duplication of information and quite seriously punish sites with similar content, up to their complete blocking in search results. Now let's imagine the following situation: your resource was indexed with the www prefix from the very beginning, but over time the domain without www received more visits and popularity (it was linked to more often and mentioned more often).

You know nothing about this and continue to develop your project. Over time, it is filled with unique content, many links and comments. Search engines, monitoring a resource without the www prefix, sent it to a ban for non-unique content. Over time, it will completely stop being indexed.

In order to avoid this situation, there is a .htaccess file. The 301 redirect contained in it can solve the problem of dividing a resource into domains with and without www.

Redirect from domain www to without prefix and vice versa

How to set up a redirect in this case. Everything is simple here. You need to open the .htaccess file and write the script below into it.

Redirect from domain www to without it:

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

And in another way:

RewriteEngine On
RewriteCond %(HTTP_HOST) ^moydomain.com
RewriteRule (.*) http://www.moydomen.com/$1.

You need to take into account that instead of moydomain.com you must indicate the address of your Internet resource in both cases!

You should save the .htaccess file and move it to the root folder and then replace the previous file. Now, whenever a visitor enters a site address (whether with or without http, with or without www), he will in all cases be redirected to the main page. We looked at how to set up a 301 redirect to from www without www. Let's now look at the other ways to use it.

Transition from one Internet project domain to another

A similar redirect scheme, which is given above, should be used when moving from one domain of an Internet project to another. To demonstrate, let's give an example. Let's say you should move from the domain moysait-1.com to moysait-2.com. In this case, you should write the following code in the .htaccess file:

RewriteEngine On
RewriteCond %(HTTP_HOST) moysait-1.com
RewriteRule (.*) http://moysait-2.com/$1.

Changing CNC links and redirecting at the same time

From time to time there is a need to change. It is quite possible that a situation has arisen that it was decided to change the entire section on your resource. Or the site owner knew nothing about CNC links, and the blog already existed and was filled with materials. In this case, redirection can help. How to set up a 301 redirect for this case? It is important that in this state of affairs, all page indicators (links, their weight in search results) will remain the same.

Let's give a simple example. It was decided to change the name of the page from moya_infa.html to obo_mne.html. In this case, you need to write the code in .htaccess: redirect 301 moya_infa.html http://www.moydomen.com/obo_mne.html.

A 301 redirect from page to page is done in a similar way.

Redirection when changing file extension

Let's give another example. The webmaster needs to change the extension of the moya_stranichka.html file to moya_stranichka.php. Similar to the previous point, we write the following code in .htaccess: redirect 301 moya_stranichka.html http://www.moydomen.com/moya_stranichka.php.

Redirect for affiliate programs

For example, you need to redirect a site visitor to a page with an affiliate program. The affiliate program link itself looks like www.partner.com/?ref=16011. Having noticed such a link, many resource visitors will not click on it, having guessed your intentions in advance.

To give the link the proper appearance, you need to create a separate page, like the example dlya_druzei.html, from which you will redirect to the page with the affiliate program. All that remains is to edit the .htaccess file: redirect 301 dlya_druzei.html http://www.partner.com/?ref=16011.

How to perform a 301 redirect in CMS Joomla

Many Internet users have probably seen a page with a 404 error. The Joomla engine already has several built-in methods for redirecting from page to page. However, they don't always work. And they are very difficult for the average user to understand. This is where the 301 redirect comes to the rescue. Joomla has a standard .htaccess file with which you can perform all the manipulations described above.

Let's set up redirections for all pages of your Internet project. First of all, you need to find the htaccess.txt file in the directory and give it the correct name.htaccess. After this, in the admin panel you should set the redirection settings to enable CNC (SEF) and check the URL redirection box.

Then you should open the .htaccess file and write the rules for the required redirections in it.

Redirect using PHP

If for some reason access to the .htaccess file does not exist or is closed, there is a way to do a 301 redirect differently. PHP will help with this.

In order to redirect from domain to domain while saving all parameters, you need to create a file with the extension .php in any text editor, call it index and copy the following script there:

if ($ref!="") $ref="?".$ref;header("HTTP/1.1 301 Moved Permanently");

header("Location: http://moydomain.ru/".$ref);

Here moydomain.ru is the domain to which the redirect is made. The resulting file must be uploaded to the server in the main directory of the site.
In order to redirect from page to page within the same domain or different ones, you need to insert the following before the main code of the transferred page:

header("HTTP/1.1 301 Moved Permanently");

header("Location: http://www.moydomain.ru/moycategory/moypage.htm");

As a result, www.moydomain.ru/moycategory/moypage.htm is the new page to which the redirect is made.

It is important to take into account that the page into which the above script is inserted must have the .php extension at the end of its name. Otherwise, you will have to use a redirect via .htaccess.

Now, after studying the article, any webmaster will know how to set up a 301 redirect, what it is and how it is useful. This shows the simplest use cases for redirects. However, any website and blog owners need to have an idea about them. And it is worth taking into account that it is important not only to configure the redirection correctly, but also to check it for correct operation.

As you can see, redirections can be created not only using the .htaccess file, but also through the standard capabilities of many CMSs, and using PHP scripts.

I decided to try it. I will post observations, practical applications and solutions to problems on the topic of SEO and “I am a webmaster” in the footsteps of my own experience. I don’t know how long I will be enough, time will tell.

Explanatory preamble: I am not a search engine optimization (SEO) specialist or a programmer. Everything described here is the experience and intuitive considerations of a more or less advanced user.

More: I would especially like to express my gratitude to ave for their repeated assistance in rehabilitating the site :)

So, on to the topic

Redirect 301: How to redirect from one page to another

The implementation of the task is simple and I knew it, although in more than 3 years of the site’s existence, 301 redirects were used only once, and not by me. But the other day I deliberately removed several pages from the site and I needed to put Redirect 301 on the old (removed) links.

What is Redirect 301?

"Redirect" in our language is "redirection", "redirection". That is, this is the code with which we communicate that we are intentionally and permanently redirecting the address (URL) of the page from the previous location to the new one. "Forever" can be canceled by deleting the forwarding.

301 - permanent redirect

302, 303 and 307 - temporary redirection

What is it for?

A simple example: you had a page with content on your site, you deleted it, it no longer exists, this means that a person who gets to this page (from Google or Yandex, for example) will see a “404 error” or “Page not found.” A bad option is that the person will immediately close your site, or will decide to find through “search” on your site what he came for, if it even exists.

In any case, seeing a “404 error” is not cool, and search engines view the massive presence of such pages on a site in an uncool way - they can lower your site in search results.


Screenshotfrom video , in which Google's Web Spam Team Leader Matt Cutts answers the question "How much PageRank is lost in 301 redirects?"

But you can’t do without deleting pages on the site, it happens, for various reasons and for various reasons they need to be set to 301.

In what cases do websites use 301 redirects?

1. to redirect one website page (link) to another
2. redirect website links with www to links without www, or vice versa
3. perform mass redirection of links in case of a change in domain or site engine

These are the main cases. My case is the first, and we are considering it.
It’s easy to do, but the simple option didn’t work for me, I had to google for a couple of hours looking for the right option.

Making a 301 redirect

This is done in the .htaccess file, which is located in the root folder of the site. Open it and enter our redirect there. I prescribed it right after RewriteBase /

save the changes to .htaccess. All. Check whether the old (not working) link is redirected to the new link.

This option didn't work for me. Result: redirection did not occur, and a parameter was added to the link address ?q=. Here's an option that worked for me. To clarify: implemented for a Drupal site

Redirect 301 - why is it needed? In the process of website promotion, a situation may arise when it is necessary to change the page url. The reasons for this may be different: moving to a new domain, gluing together duplicate pages, or even, not to mention, sanctions from search engines. To change the url, a redirect is used, properly called "301 Permanent Redirect".

What is a 301 redirect?

301 redirect operates on an ongoing basis, transfers link juice, TIC of the site, etc. from the old page to the new one. Also, if the site is in the Yandex or DMOZ directory, the pages of the new site, of course, will not be there. In addition, a 301 redirect to another site is used, and redirection from one version of the site to another. In particular, this gets rid of problems during indexing that arise due to duplication of site pages.

Getting rid of duplicate pages is also called canonicalization.

How to set up a 301 redirect

When using some CMS (content management systems, or website engines), the redirect can be configured using special plugins. However, the more common practice is to set up a 301 redirect by making changes to the .htacess file.

Several examples of using 301 redirects

Gluing website mirrors

For a search engine, site.name and www.site.name are different sites. One of them will be indexed, the second will be a duplicate. To prevent this from happening, they do the so-called “gluing” of the site. From a site with “www”, a redirection to a site without “www” is indicated. This is done in the .htaccess file using the following construction:

RewriteEngine On RewriteCond %(HTTP_HOST) ^www.vash-sait\.ru$ RewriteRule ^(.*)$ http://vash-sait.ru/$1

If this was not done at the initial stage, and the version of the site with “www” is included in the index, it is worth doing a reverse redirection.

RewriteEngine On RewriteCond %(HTTP_HOST) ^vash-sait\.ru$ RewriteRule ^(.*)$ http://www.vash-sait.ru/$1

Redirect to a page with a different url

When using CNC links, sometimes a situation arises when the url needs to be changed, but the original page is already in the index. In order not to lose position and weight when changing a link, it is worth setting up a redirection from the original link to the changed one.

RewriteEngine On RewriteRule ^(.*)url.html$ http://vash-sait.ru/new-url.html

Redirect to another domain

In a situation where it is necessary to change the domain, this can be done without losing positions and weight. To do this, you need to install a 301 redirect from the old domain to the new one.

RewriteEngine on RewriteCond %(HTTP_HOST) ^www\.staryy-sait\.ru$ RewriteRule ^(.*)$ http://novyy-sait.ru/$1 RewriteCond %(HTTP_HOST) ^staryy-sait\.ru$ RewriteRule ^(.*)$ http://novyy-sait.ru/$1

Redirecting aliases to the main site domain

If a site uses several domains (aliases) registered in different domain zones, then in order to avoid duplication of content, you need to install a 301 redirect to the main domain.

RewriteEngine On RewriteCond %(HTTP_HOST) ^vash-sait.com$ RewriteCond %(HTTP_HOST) ^www.vash-sait.com$ RewriteCond %(REQUEST_URI) !^/robots.* RewriteRule ^(.*)$ http:// vash-sait.ru/$1

Removing the extension using 301 redirects

RewriteEngine on RewriteRule ^(([^/]+/)*[^.]+)$ /$1.html [L]

Redirect from index.php to home page

In some cases, the main page of the site is available at several addresses at the same time. This also has a negative impact on promotion, as it indicates duplication of content. You can get rid of this by using the following construction:

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

Pages with a slash in their URL and pages without this character can also be considered duplicates. The following code sets up a redirect from pages without a slash to pages with a slash:

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

Redirect for url with parameters

If the source address contains any parameter (for example, http://vash-sait.ru/articles.php?section=1. The parameter here is “section=1”), then the redirecting structure will look like this:

RewriteEngine On RewriteCond %(QUERY_STRING) section=1 RewriteRule ^index.php http://vash-sait.ru/articles.php?