How to set up redirects and when to use them

SEO-specialists and developers often have to deal with redirects. At the same time, the developers often don't understand how to configure them and why are they needed.

What are redirects?

Redirects are a way to redirect website users to URLs other than what they requested. Page duplicates worsen the website ranking, but redirects help to combat them.

They are also used when you need to create a new page to replace an existing one and maintain the website position and traffic at the same time. There are nine types of redirects, but only three of them are actively used in SEO practice:

  • 301 Moved Permanently (permanent redirect) indicates that the document has been moved to the new URL forever. When SEO specialists or developers talk about a redirect, they almost always mean a 301-redirect. The old URL disappears from the search results and is replaced by a new one.
  • 302 Found, 302 Moved Temporarily (found, moved temporarily) indicates that the document is transferred temporarily, for example, takes the user to the page of the action (sale), without changing the contents of the old page. At the same time, the old page URL remains in the search results, because it has been temporarily moved to the new one.
  • 307 Temporary Redirect means that the requested document is available for a short time at a different URL while retaining the request transmission method (GET, POST). Basically, it performs the same task as the 302-redirect.

According to John Mueller, Google may not distinguish between response codes 301 and 302 since all options transmit 100% of the link weight and PageRank from the old URL to the new. Therefore, if Google decides that you installed the 302-redirect by mistake, it will treat it as a 301-redirect.

General tips for setting up redirects

1. Setup redirects only to relevant pages with a 200 status (OK)

The more relevant the acceptor page (the one that accepts the redirect) to the donor page, the faster they will stick together, and the more weight it will transfer.

2. Avoid using a redirect where you can set a rel = canonical

If the content of the pages is duplicated, but it's important for you to leave the pages accessible to users, use rel = canonical instead of a redirect. For example, if there are several versions of the content (for printing, mobile, etc.)

3. Don't use redirects for robots.txt

It's important when changing a domain or moving to a secure protocol; the old robots.txt is accessible to robots, so domains are stick together faster.

4. Avoid two, three or more redirects in a row

Each new redirect means loss of load time, extra load on the server, and possible loss of the transmitted page weight.

5. First use page redirects with a higher level of nesting in the .htaccess file , for example:

  • first goes the redirect from site.com/category-1/subcategory-1/ to site.com/category-1/subcategory-2/;
  • then from site.com/category-0/ to site.com/category-0;
  • and the last go global rules such as redirecting all pages without "/" to pages with "/".

6. Browsers cache redirects: reset cache or use specialized services to check their operation.

Although the link weight is completely transmitted, there is a high probability of losing positions and page traffic by 10-15%, with subsequent recovery within 2-4 weeks with the massive use of redirects, for example, when moving to https.

Some SEO-experts advise you first to configure 302-redirects, and after the newest pages appear in search results, change it to 301 in order to exclude the possibility of losing traffic.

Please be warned that such a use of the 302-redirect is contrary to the recommendations of Google, but no one forbids you to do experiments :)

.htaccess syntax and characters

. — dot means any character;
[abc] - a list of characters matching the letters a, b, or c;
[^ abc] - a list of characters that are not in the range. The condition matches any character except a, b, c;
* - the previous character can be repeated 0 or more times;
[abc] * - find characters from a given set, going in a row;
[^ abc] * - inverse operation;
. * - replacement of any character set;
 ".*" — find all substrings between quotation marks.
^ - indicates the beginning of a line (when used at the beginning of an expression);
$ - end of a line;
\ w - letter, number or underscore _;
\ d - any digit;
\ D - any character except digits;
[0-9] — an indication of any digit;
[a-z] - an indication of any letter from a to z with the lower case;
[A-Z] - an indication of any letter from A to Z with the upper case;
[a-zA-Z] - any letter from a to Z, the case is not important;
[a-Z] - the same thing, only shorter.
Flags for additional options
NC - NoCase disables character case checking when a rule is triggered.
R - Redirect stops the URL change and returns the result.
L - Last stops the creation of the URL, and the line is considered final.

301-redirect: .htaccess examples

The examples are given for Apache servers; these settings will not work for NGINX.Below you can find situations where there is a need to configure a 301-redirect, and code examples for .htaccess . We recommend that you make a copy of the .htaccess file before making changes in it.

1. To determine the main website mirror

Redirect from a version without www to a version with www

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

Redirect from version with www to version without www

RewriteCond %{HTTP_HOST} ^www.site\.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]

Both options are equivalent and correct from the point of view of search engines. If you have more pages with www in the index, it would be better to use redirects to the version with www, and vice versa.

2. Canonicalizing the slash at the end of the URL

— http://www.site.com/dog1/

— http://www.site.com/dog1

If the page URL differs by at least one character, search engines consider such pages to be different. And if these are different pages with the same content, then they are duplicates.

Therefore, it's extremely important to ensure that all page URLs are only lowercase and of the same format (with a slash or without a slash at the end). As in the case with www , set up redirects to the version of pages with which you have more in the index.

Redirect to remove "/" at the end

RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /$ [NC]
RewriteRule ^(.*)(/)$ $1 [L,R=301]

Redirect to add "/" at the end of the address bar

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

3. Redirect from any URL to lowercase URL

$lowerURI=strtolower($_SERVER['REQUEST_URI']);
if($_SERVER['REQUEST_URI']!=$lowerURI)
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://" . $_SERVER['HTTP_HOST'] . $lowerURI);
exit();
}

Search engines are case sensitive, it's important to use a single case for all characters in the URL so to avoid duplicates.

4. Redirect from all pages of one domain to the main page of another

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !(^$|.*\.(css|jpg|gif)) / [R=301,L]

This setting may be useful if you have a good old domain, which does not fit the topic or structure at all, but you would like to transfer the weight from it to your domain.

5. Redirect when moving to https

RewriteCond %{HTTPS} =of
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Just like with slashes and the main mirror, the main goal of such a redirect is to combat page duplicates. Additionally, when moving to https, we recommend using HSTS .

You need to understand that when setting up redirects to https, the ability to send http requests to the website is still activated. HSTS tells the browser not to "communicate" with the website through http.

Even if the browser encounters an http link on this website, or the user enters it manually, he will send the request immediately to the https version and will not go the way when the request is sent first to the http version, and only then the server itself uses a 301redirect to https. This is done to protect the user from possible wedges and redirects of cool hackers and intruders. Search bots will not fall under this scenario.

6. Redirect when changing the directory containing documents

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

This rule is usually used if the directory structure is displayed in the URL, and you need to change or leave it (for example, to add a keyword to the URL or shorten it).

7. Redirect from one page to another

Redirect 301 /oldpage.html http://site.com/newpage.html

We use it if the document URL has changed, or if there is no longer a service/product, but there are prototypes to transfer users and weight to these pages.

8. Stick several characters together in a row into one character (for example, stick two hyphens into one):

RewriteCond %{REQUEST_URI} ^(.*)--(.*)$
RewriteRule . %1-%2 [R=301,L]

Most often, these are a few slashes or hyphens in a row. Such pages may appear due to incorrect configuration of routers, for example, two spaces in a row are not combined and turn into two hyphens, or the router always adds "/" at the end, without checking if it's there or not.

9. Redirect for pages with GET parameters in URL

For example, the page URL is http://site.com/catalog/index.php?IBLOCK_ID=4&SECTION_ID=20. In order to make redirect:

RewriteCond %{QUERY} ^IBLOCK_ID=4&SECTION_ID=20$ [NC]
RewriteRule ^catalog/index\.php$ /newcatalog/? [R=301,L]

Sometimes it's necessary to set up a redirect for changing Get parameters. For example, the parameter would be SECTION_ID:

RewriteCond %{QUERY} ^IBLOCK_ID=4&SECTION_ID=(.*)$ [NC]
RewriteRule ^catalog/index\.php$ /catalognew/? [R=301,L]

We use both types of redirects mainly when it's necessary to configure the user-friendly URL instead of the URL with parameters.

10. Forwarding a single URL without nested addresses

If you need to configure a redirect from the section page http:/site.com/category/, and other pages in the section should work without a redirect, for example, the page http://site.com/category/post-1/, there should be added only one character, i.e., $.

RewriteRule ^category/$ http://site.com/new-category/ [R=301,L]

For example, if most of your website's documents are in the catalog folder and the site.com/catalog page does not contain any content.

302 and 307-redirect: how to use it correctly

Using a 302-redirect is justified in the event:

  • It's important that the original page continues to be indexed. For example, you have an online store, and some product has ended, but there is a very close analogue of it. Then, while the product is out of stock, you can redirect users to the page with its analogue.
  • The landing page will frequently change. For example, for requests such as "concerts in NY today," you can make 302-redirects to a page with the current date on the website every day.
  • Links are placed on the original page, which should be further indexed.

Don't forget: if Google considers the use of the 302-redirect in a particular case to be erroneous, the old URL will disappear from the search results, and the link weight and PageRank will completely switch to the new one.

The rules for using 302-redirects in .htaccess are similar to the described rules for using 301. You just need to replace R = 301 with R = 302.

For search engines, a 307-redirect is an almost complete prototype of 302.

The only difference is that the 307-redirect saves the request transmission method, while 302 can behave unpredictably with methods other than GET (i.e., POST, PUT, DELETE).

Conclusion

Each redirect should be used to solve the problem for which it was created, follow the recommendations of search engines:

  • if you need to permanently change the page or transfer the link weight you can use 301-redirects;
  • if the landing page changes frequently or redirects for a short time use a 302-redirect;
  • if the request transmission method is not GET, and it's important for you to process it correctly, use 307-redirects instead of 302.
Table Of Contents
Follow