This site uses cookies and other tracking technologies to make possible your usage of the website, assist with navigation and your ability to provide feedback, analyse your use of our products and services, assist with our promotional and marketing efforts, and provide better user experience.

By using the website, you agree to our Privacy policy

Accept and continue

Report a bug

Cancel
43
How-to 10 min read September 26, 2019

Configuring redirects on a website: how to avoid mistakes

It is important to monitor the use of redirects for the correct website operation. Necessary redirects can be registered in the .htaccess file, and you can also add a JS, HTML or PHP redirect to the page code.

What is a redirect

A redirect is an automatic redirection of a user from one page of a website to another which is carried out by the server. It can occur both within one domain and redirect to another website.

Redirects are used for the following purposes:
1
To set up redirection of all visitors after changing the domain. This is important so as not to lose the entire permanent audience of the website who are used to finding it at a specific URL. Also, such a redirect allows you to save all the results of the resource SEO.
2
For the purpose of the SEO, in order to "glue" domains, that is, to indicate to the search engine the preferred version of the site, for example, with www or without www. All visitors will be redirected to the option specified using the redirect, regardless of how they entered the website URL.
3
When changing the address format, you must also specify a redirect so as not to lose the existing traffic and optimization results. For example, when changing the address of "some-page.html" to "some-page" without an extension.
4
When switching to the secure HTTPS protocol.
5
When deleting a page from a website. To make sure the user does not get to the page with a 404 error, he will immediately be redirected to a page with similar or the same content (if the URL is changed).
How to configure redirects:

  • using the .htaccess file in the root directory of the website;
  • in the control panel of the hosting provider;
  • on the page of a website using JS, PHP or HTML code.

Basic errors related to redirects

  • multi-step redirects ("redirect chains") to pages where redirects are already configured. The redirect should lead to the desired page without a subsequent redirection;
  • redirects to irrelevant pages. Redirects should lead to the content requested by a user. You must not configure redirects to a page that has significant differences from the original one, for example, to a completely different product in an online store;
  • redirects to deleted or inactive pages. You can only configure redirects to pages with a server response 200 which indicates their correct operation;
  • redirects for doorways, i.e. websites that are optimized for a number of queries and have no user value. These resources are created specifically for redirecting users to other websites and the use of them contradicts recommendations of search engines;
  • configuring a redirect instead of rel=canonical. If the page content is duplicated, for example, in print or mobile versions, it is preferable to specify a canonical address;
  • a very significant mistake is to configure a "302" redirect instead of a "301" redirect when changing a domain, deleting a page or changing a URL.

Redirect types

  • 300 Multiple Choices — multiple choices. There are several addresses to which a user is redirected depending on their choice or the browser settings:
Redirect 300 Multiple Choices
This redirect option is rarely used;

  • 301 Moved Permanently is a permanent redirect to a new address. This is the most popular redirect type removing the old address from the index, but retaining all its parameters and passing them to the new one. This option is also suitable for removing duplicates. You can apply it if you no longer need to use the current page or domain;
  • 302 Found is a temporary redirect to another address. A "302" redirect is leading you to a new page that is not indexed, and all results stay at the old URL. This type is not suitable if you change a domain;
  • 303 See Other is a redirect to a page that uses the GET method to display it. This redirect indicates that the document was found, but you need to use the GET request method to go to it even if the HEAD or POST were originally used. It is rarely used, for example, when one document was found as a result of a user search on a website. In this case, you can immediately redirect the visitor to it using a "303" redirect without showing the search results separately;
  • 304 Not Modified is a redirect indicating that the document has not been changed. This is a response the browser receives when it re-accesses a page that has not been modified. In this case, it must be downloaded from the browser cache;
  • 305 Use Proxy is a redirect to a page which is performed through a proxy; its address is transmitted to the browser;
  • 306 Switch Proxy is not used at the moment. This redirect previously meant that future requests should occur through the transmitted proxy;
  • 307 Temporary Redirect is a redirect similar to a "302" redirect by its activity. It also temporarily replaces the address of the original version which is not removed from the index. Unlike a "302" redirect, it eliminates the ambiguity about whether to change the document request method.

How to configure redirects

Let us consider in various ways the configuration of the 301 redirect in detail which is most often used in SEO. All other redirect codes can be specified in the same way if necessary.

How to specify a "301" redirect in .htaccess

A "301" redirect can be specified in the .htaccess file located in the root folder of the website. You can edit this file in the file manager of the hosting provider:
301 redirect via htaccess
A rule should be written at the beginning of the file that indicates to the server that there are redirects:
Setting redirects in htaccess file
A redirect of one page:
301 redirect pages in htaccess
A redirect of the whole website:
301 redirect site in htaccess
A redirect from WWW to a version without WWW, that is, a redirect specifying a mirror:
Specifying a mirror in htaccess
A redirect from HTTP to HTTPS:
Redirect from http to https in htaccess
Redirect from http to https in htaccess:
Redirect with a slash in htaccess

Configuring a "301" redirect via scripts

PHP Redirect

To redirect the entire website, the following code must be placed in the index.php file:
<?php 
 header("HTTP/1.1 301 Moved Permanently"); 
 header("Location: https://www.new-site.ru"); 
 exit(); 
 ?>
When redirecting particular pages, you can use exactly the same construct by placing the code at the top of the document.

JS Redirect

You can use various JavaScript functions to configure redirects. Regardless of the implementation you choose, you must place the code inside the <script> </script> tags on the HTML page. A JS redirect requires that the source page from which the redirect is configured continues to exist. Here are the options of the redirect functions:

  • document.location="https://www.new-site.com";
  • window.location.replace("https://www.new-site.com");
  • window.location.reload("https://www.new-site.com");
  • document.location.replace("https://www.new-site.com");
  • location="https://www.new-site.com/";
  • a redirect after 10 seconds with a redirect message to a user:
function Redirect() 
    {  
        window.location="https://www.new-site.com"; 
    } 
    document.write("Our website has a new address, you will be redirected to it in 10 seconds"); 
    setTimeout('Redirect()', 10000);
HTML Redirect

The code is written in the <head> element; a redirect occurs in 5 seconds:
<head>
<meta http-equiv="refresh"="5;URL=https://www.new-site.ru" />
</head>
This option is not recommended by W3C which advises you to redirect users in other ways.

Configuring redirects in the hosting control panel

You can also configure redirects in the website's settings on the hosting. In this case, you can activate redirects to the secure HTTPS protocol and to the address format without WWW:
Setting redirects on hosting
When using such settings you need to consider whether there are such parameters in the CMS so as not to duplicate redirection rules or to establish conflicting redirects.

Conclusion

  • Using redirects allows you to redirect visitors when changing a domain, set redirects to similar products in the online store and fight duplicates;
  • most often a "301" redirect is used for search engine optimization, as well as a "302" and a "307";
  • technically you can implement redirects using the .htaccess file, or by placing the necessary HTML, JS or PHP codes on the website pages.
This article is a part of Serpstat's "Site Audit" tool
SEO Audit in Serpstat
Audit all the site or page in one click. A complete list of errors, sorted by severity, ways to resolve them and recommendations. Any frequency of verification and automatic email reports.
Run Site Audit

Learn how to get the most out of Serpstat

Want to get a personal demo, trial period or bunch of successful use cases?

Send a request and our expert will contact you ;)

Rate the article on a five-point scale

The article has already been rated by 0 people on average out of 5
Found an error? Select it and press Ctrl + Enter to tell us

Share this article with your friends

Sign In Free Sign Up

You’ve reached your query limit.

Or email
Forgot password?
Or email
Back To Login

Don’t worry! Just fill in your email and we’ll send over your password.

Are you sure?

Awesome!

To complete your registration you need to enter your phone number

Back

We sent confirmation code to your phone number

Your phone Resend code Queries left

Something went wrong.

Contact our support team
Or confirm the registration using the Telegram bot Follow this link
Please pick the project to work on

Personal demonstration

Serpstat is all about saving time, and we want to save yours! One of our specialists will contact you and discuss options going forward.

These may include a personal demonstration, a trial period, comprehensive training articles & webinar recordings, and custom advice from a Serpstat specialist. It is our goal to make you feel comfortable while using Serpstat.

Name

Email

Phone

We are glad of your comment
Upgrade your plan

Upgrade your plan

Export is not available for your account. Please upgrade to Lite or higher to get access to the tool. Learn more

Sign Up Free

Спасибо, мы с вами свяжемся в ближайшее время

Invite
View Editing

E-mail
Message
Optional
E-mail
Message
Optional

You have run out of limits

You have reached the limit for the number of created projects. You cannot create new projects unless you increase the limits or delete existing projects.

I want more limits