As the name indicates, redirect is a technique to route search engine crawlers and visitors to an available webpage when a requested webpage is not available. There are many ways to setup redirect from a old page to a new page. When redirect is setup, the web server will return a 3xx HTTP status code. During troubleshooting you can find the status code to understand the type of redirect used. In this article, we will explain what is 301 redirects and how to setup redirects for your site to optimize for search engines.
What is 301 Redirect?
Out of all 3xx redirects, “301 – Permanently moved” is the most commonly used redirect by webmasters. In order to avoid 404 not found error it is necessary to setup a 301 redirect whenever the URL of an existing webpage is changed or deleted. This will inform search engines to replace the old URL with the new one and serve the users with the new one whenever requested.
301 is the HTTP status code returned by a web server when the requested URL by the client is reached by redirecting the user agent. The user agent is normally a web browser or a search engine crawler trying to access a web page. 301 status code is not shown in the browser window and the user agent is automatically redirected to another resource by the server. Hence, it is generally called server side 301 redirect setup.
The pictorial representation of 301 redirect is shown in the below picture:
How to Check 301 Status Code?
If you want to test your 301 redirect then the simple way is to open the old URL in the browser window. You should be automatically redirected to your new URL if you setup the redirect correctly. You can also use “Fetch as Google” or “Fetch as Yandex” tool available in the webmaster tools account to check 301 status code is received from the server for the old URL.
Why do You need 301 Redirect?
301 redirect is a useful tool for diverting users from your old URL to your new URL. This may be required in one of the following situations:
- To avoid 404 not found error in situations like change in the existing page URL, an old webpage is deleted or merging two sites.
- Canonical URLs to use www or non-www version.
- Transferring domain from one service provider to another.
In all the cases setting up of 301 redirect is an important task in order to avoid losing the hard-earned traffic to your site.
1. To Avoid 404 Not Found Error
When you build a large site with hundreds of pages, it is a common mistake that some of the pages may not linked appropriately. Sometimes it is also possible that an external link in your site becomes invalid due to non-availability of that page. Both cases will result in a broken link and when clicked on a broken link the server will respond with an HTTP code “404 – Page Not Found”. Page not found is a common message when the URL of a webpage is changed and the old link has not been redirected to the new page.
Search engines count these 404 errors as a problem in your site since it can’t offer a requested resource to the users. For example, Google Search Console shows all 404 error messages in your site’s dashboard as a crawl errors. You will also lose your ad revenue in case if you placed any ads on the pages not available to search engines.
It is also possible that user entered a wrong URL or use to access some sub-directory which does not exist. The simple way to get advantage over 404 error is to create your own “404 – Not Found” page and provide useful information like search box and important links in your site. This will help users to search or navigate through your site and get the required information.
2. Redirect between www and non-www Versions
One of the important need of setting up a redirect is to direct all your traffic to one source, that is www or non-www version of your site. For example:
- You registered a domain name example.com and did not set any preferred domain in Google webmaster tools account.
- Google treats https://example.com and https://www.example.com as a two different sites and considers the links for each site separately.
This will really affect your SEO ranking of your site since the link count will be reduced for any version of your site. For a new site, set your preferred domain as either https:// or https://www in webmaster tools account, submit Sitemap of that version and ensure Google indexes all your pages of that version. In case if you have a running site with split of inbound links then set a redirect of one version to another version of your site.
3. Domain Change and Transfer
Setting a redirect is required whenever there is a change in a site name or changing service providers. For example:
- Initially you may start building your site as a free site with a name like https://example.WordPress.com and later want to change it to https://example.com.
- You started building a HTML site and later decided to go for PHP service provider.
If you have different version of your site for desktop and mobile then it is essential to setup a mobile redirect in your desktop site so that when users opening your site in a mobile devices will be redirected to your mobile pages.
How to Setup 301 Redirect in Various Platforms?
It depends on the hosting service provider and the type of website you are using. For example, Weebly provides a simple way to setup redirects without changing any root directories if the old and new domains are same. On platforms like WordPress, you can use plugins to setup redirects with ease. Otherwise you may need to follow any one of the methods listed below for setting up 301 redirects.
All the below examples indicate setting a redirect in an old page in order to redirect to a new page within the same domain. If your domain name is changed then replace the domain name with your new domain. Generally redirection is done at server side by configuring Apache or IIS servers to ensure the server returns 301 HTTP status codes for search engines. This is done using PHP, ASP or ASP.NET scripts. Client side redirects using HTML meta refresh or JavaScript are not recommended since these methods will not return 301 code.
Server Side 301 Redirect
PHP
Save this change in your old php page.
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.yoursite.com/newpage.php");
exit();
?>
In PHP redirect, the old page should be with .php extension and the new page can be with any extension. If you want to use PHP redirect with an existing HTML page then enable the following line in your .htaccess or httpd.conf file.
Addtype application/x-httpd-php .htm .html
ASP
Save this in your old asp page.
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", " https://www.yoursite.com/newpage.html"
%>
ASP.NET
Save this in your oldpage.aspx.
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location"," https://www.yoursite.com/newpage.html");
}
</script>
.htaccess Redirect for Apache Server
In order to set 301 redirects in an Apache server you should have access to the configuration file called .htaccess. Both single page redirect as well as domain level redirect can be set using .htaccess file.
Redirecting old page to a new page:
Redirect 301 /oldpage.html https://www.yoursite.com/newpage.html
In order to redirect all pages of an old domain to a new domain configure .htaccess file of an old domain as follows:
Redirect 301 / https://www.newdomain.com/
Redirecting Canonical Sites Using .htaccess
https://www.yoursite.com and https://yoursite.com are considered as a two different sites by search engines and hence it is recommended to choose a preferred domain settings in your webmaster tools account. You also can redirect all traffic to a site with www by modifying .htaccess file with the following code:
RewriteEngine on
rewritecond %{http_host} ^coolexample.com [nc]
rewriterule ^(.*)$ https://www.yoursitename.com/$1 [r=301,nc]
301 Redirect for IIS
- Choose the file in the Internet Services Manager for which redirection is to be set.
- Right click and choose redirection to a URL.
- Enter the new file name for redirection.
- Choose “The exact URL entered above”.
- Select “A permanent redirection for this resource”.
- Click Apply.
Client Side 301 Redirect
HTML Meta Refresh
Though HTML meta refresh does return a 301 HTTP status code this is considered as a 301 redirect by search engines like Google. Replace the old code of your HTML page with the below code in order to redirect to a new page.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh"
content="0; url= https://www.yoursite.com/newpage.html">
</head>
<body>
<p>This page is permanently moved
<a href=" https://www.yoursite.com/newpage.html">Here</a></p>
</body>
</html>
JavaScript Redirect
Add the following code in your old page to redirect to a new page. Like HTML meta refresh, JavaScript redirect also will not return 301 code.
<html>
<body>
<script type="text/javascript">
// Javascript URL redirection
window.location.replace("https://www.yoursite.com/newpage.html");
</script>
</body>
</html>
jQuery Redirect
This is another type of redirect without returning 301 code, add the below code in your old page:
<!DOCTYPE html>
<html>
<body>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// jQuery URL redirection
$(document).ready( function() {
url = "https://www.yoursite.com/newpage.html";
$( location ).attr("href", url);
});
</script>
</body>
</html>
What is an Infinite Loop?
Generally a web server can redirect the user agent up to five times to fetch the requested resource and redirecting more than five times will result in endless loop called infinite loop. In this case the user agent may try to fetch the resource without any result.
Infinite loop also will occur by not setting up the 301 redirect appropriately. For example, when two pages “https://example.com” and “https://example.com/index.html” are pointing to the same home page of the site then setting up 301 redirect of any one URL to others will end up in an infinite loop.
Leave a Reply
Your email is safe with us.