How to configure 301 redirect via .htaccess. The right attitude as a 301 redirect in Joomla and other CMS

Many have heard about the 301 redirect 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. The article will discuss in detail how to configure 301 redirects.

About its configuration, you can find more than a dozen articles and informational materials. But after viewing many of them, novice computer users and webmasters may have difficulty understanding the basics of customization. Numerous codes and scripts provided in online sources of information, for the most part, cause misunderstanding. In reality, everything is much simpler, and this article will serve as a good guide on how to set up 301 redirects.

What is 301 redirect?

Let's figure it out in order. First of all, let’s define the redirect 301. By it we mean the simplest redirection of a guest of a resource on the Internet (site) to another page on the network or a domain name. Immediately make a reservation that this article will not discuss any tricky scripts and code that is too complicated, but a few simple redirection options that are sufficient basic knowledge on this topic for any webmaster will be shown.

301 redirect tool - .htaccess file

Consider a handy tool like .htaccess. Redirect 301 through it is very convenient and easy to do. Htaccess is inherently a special utility file in the configuration, which contains very great functionality and many features. Using it, you can set a ban on access to any directories on a site server, specify page encoding, set protection for folders and files, perform 301 redirects and some other actions.

how to configure redirect 301

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

Procedure for creating a .htaccess file

If this file is not in the root folder of the site, it can be created very easily on your own. One of the fastest ways to create is using an FTP client. The most convenient of them is FileZilla.

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

htaccess redirect 301

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 its presence in hidden files. To do this, go to the “Server” tab and check the box for forced display of hidden files.

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

The most popular case when a 301 redirect can come in handy

According to the recommendations of leading webmasters, each resource on the Web should have a 301 redirect. And the most popular case when it is really needed is a redirect from a domain, which in its composition has a prefix from www, to a domain without it and vice versa.

This is because any website on the Internet only has one type of domain name with www or without it can be the main one. Another option will be a mirror of the site. The main mirror of the Internet resource is signed in the robots.txt file using the Host directive. In addition to this file, the main variant of the domain should be indicated in Yandex.Webmaster. Many search engines relate well to a tool like 301 redirects. Yandex is no exception.

how to configure 301 redirect

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

Site Mirror Setting Value

After all the actions done with the mirror, the search engines will automatically determine the main domain. You can even easily check the redirect, everything will work well. We could stop on these steps, but the best way for SEO-optimization of the Internet resource would be to configure the redirect additionally.

The www prefix itself in the designation should have long gone into oblivion. Nevertheless, on the Internet there are still many resources with www in the title. For search engines, domains with www and without it are two different addresses. Yes, and many users, when searching for a specific Internet project, drive the option from www into the address bar. Therefore, this prefix has a place to be. Based on this fact, setting up mirrors is a necessary action. And the guide on how to set up 301 redirects is also a must-have for learning to successfully index your web project in search engines.

The consequences of improper mirror settings

In order to appreciate the full significance of the 301 redirect and the correct mirror settings, we highlight the harmful consequences of their incorrect operation. There are not many of them, just two. But the weight of each item is very high.

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

how to configure 301 redirect to from www without www

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

You do not know anything about this and further develop your project. Over time, it is filled with unique content, many links and comments. The search engines, monitoring the resource without the www prefix, sent it to the ban for non-unique content. Over time, it will completely stop being indexed.

In order to avoid this situation, a .htaccess file exists. The 301 redirect, prescribed in it, is able to solve the problem of dividing a resource into domains with www and with its absence.

Redirect from www domain to site name without prefix and vice versa

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

Redirect from www domain to the name of the site without it:

Rewriteengine on
RewriteCond% {HTTP_HOST} ^ www.moydomen.com
RewriteRule ^ (. *) $ Http://moydomen.com/$1 [R = 301, L].

And in another way:

Rewriteengine on
RewriteCond% {HTTP_HOST} ^ myydomain.com
RewriteRule (. *) Http://www.moydomen.com/$1 [R = 301, L].

Please note that instead of moydomen.com you must specify the address of your Internet resource in both cases!

redirect 301 what is it

You should save the .htaccess file and transfer it to the root folder with the subsequent replacement of the previous file. Now, the visitor will be redirected to the main page in any case with any driving in the website address (even with http, even without it, even with www, even without it). We looked at how to configure 301 redirects from www without www. Let's now figure out the rest of the ways to use it.

Transition from one domain of an Internet project to another

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

Rewriteengine on
RewriteCond% {HTTP_HOST} moysait-1.com
RewriteRule (. *) Http://moysait-2.com/$1 [R = 301, L].

Change of CNC links and redirect at the same time

At times, it becomes necessary to change the CNC links. It is quite possible that there was such a situation that it was decided to change the entire section on your resource. Or the site owner did not know anything about CNC links, and the blog already existed and was filled with materials. In this case, redirection may help. How to configure a 301 redirect for this case? It is important that in this situation, all the indicators of the pages (links, their weight in the search results) will remain the same.

redirect 301 php

We 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 the file extension

We give one more example. The webmaster needs to change the extension of the file moya_stranichka.html to moya_stranichka.php. Like the last paragraph, we write the following code in .htaccess: redirect 301 moya_stranichka.html http://www.moydomen.com/moya_stranichka.php.

Affiliate Redirect

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

To give the link the proper look, you need to create a separate page, as an example dlya_druzei.html, from which a redirect to the page with the affiliate program will be performed. It remains only to edit the .htaccess file: redirect 301 dlya_druzei.html http://www.partner.com/?ref=16011.

Further in the article, we will consider how to redirect 301 in the Joomla engine and using the PHP language.

How to redirect 301 in CMS Joomla

Many Internet users have probably observed a 404 error page. Joomla engine already has several built-in redirect methods from page to page. However, they do not always work. And they are very difficult to understand by the average user. Here the 301 redirect will come to the rescue. Joomla has in its standard structure a .htaccess file with which you can perform all the manipulations described above.

redirect 301 joomla

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

Then you should open the .htaccess file and write down the rules for the necessary redirects 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 make 301 redirect differently. PHP will help with this.

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

<? $ ref = $ _ SERVER ['QUERY_STRING'];

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

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

exit ();

?>.

Here moydomain.ru is the redirected domain. 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, you need to insert the following before the main code of the portable page:

<? php;

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

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

exit ();

?>.

As a result, www.moydomain.ru/moycategory/moypage.htm is a new page that is redirected to.

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

Now, after studying the article, any webmaster will know how to configure the 301 redirect, what it is and how it is useful. The simplest use cases of redirects are shown here. However, it is necessary to have an idea about them to any owners of sites and blogs. And it is worth taking into account that it is important not only to configure the redirection correctly, but also to check it for proper operation.

As you can see, redirects can be created not only using the .htaccess file, but also through the standard features of many CMS, and using PHP scripts.

Source: https://habr.com/ru/post/C5612/


All Articles