Pull to refresh

Redirects: 301, 302, 307 | How-To 301 guide

Reading time6 min
Views14K

What is redirection?


Redirection is a way of forwarding the user to the URL that is different from the one they initially clicked on. Down below some of the most common types of redirection are listed.

how to do a 301 redirect

301 Moved Permanently


A 301 redirect is a permanent redirect which passes between 90-99% of link equity (ranking power) to the redirected page. 301 signals that the page has been moved to another URL and the old URL is outdated.

302 Found (HTTP 1.1) / Moved Temporarily (HTTP 1.0)


302 is a temporary redirect which passes 0% of link equity, and shouldn't be used in most cases.
As of now, the internet runs on an HTTP protocol which dictates how URLs work. In two versions of this protocol, the HTTP response status code is different:

  • HTTP 1.0: 302 status code is «Moved Temporarily» — current page has been moved temporarily to another URL.
  • HTTP 1.1: the description phrase has been changed to «Found» — current page has been found.

307 Moved Temporarily (HTTP 1.1 Only)


A 307 redirect is the HTTP 1.1 successor of the 302 redirect. While the major crawlers will treat it like a 302 in some cases, it is best to use a 301 for almost all cases. The exception to this is when content is really moved only temporarily (such as during maintenance) and the server has already been identified by the search engines as 1.1 compatible.

Since it's essentially impossible to determine whether the search engines have identified a page as compatible, it is generally best to use a 302 redirect for content that has been moved temporarily.

Other redirection types


There are also some other types of redirection: Meta Refresh or JavaScript redirection that are executed on the page level rather than the web server level. This is what a typical Meta Refresh redirect looks like:

<meta http-equiv="refresh" content="2;url=http://example.com/" />

It's best not to use these types of redirect, as they are often used by spammers and doorway pages. Besides, they pass little to none of the link juice.

Examples of using redirects


Redirecting your domain to a non-www URL:

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

Redirecting your domain to a www URL:

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

To choose which URL to make canonical, consider:

  • which URL ranks higher in the SERPs;
  • which URL is more represented in the index.

Redirecting your domain to URL without a slash

When developing a website, it's important to choose whether you want to add a slash to the links, because the search engines consider the links
www.site.com/cat1
and
www.site.com/cat1
to be different. Then, you'll have to add the following code:

To delete the slash from the URLs:

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

To add the slash to the URLs:

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

To redirect the user from one page to another:

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

Redirecting the main page duplicates

This code ensures that if there are multiple versions of the direct link to the main page (index, etc.), they will all redirect to the canonical main page:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index|main)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.site.com/$1 [L,R=301]

Redirecting catalogs

If the URLs reflect the structure of a catalog, changes in the catalog will lead to changes in the URLs. In this case, use the following redirect:

RewriteRule ^(.*)/old-catalog/(.*)$ $1/new-catalog/$2 [R=301,L]

But, if the URL of the previous catalog comes right after the name of a domain: www.site.com/old-catalog, use this code:

RewriteRule old-catalog /(.*) / old-catalog /$1 [R=301,L]

If you've switched platforms or a CMS and only the URLs' extension has changed, use this redirect:

RedirectMatch 301 (.*)\.php$ http://www.site.com$1.html

Examples of using redirection to avoid duplicate pages

Redirecting (sub-)domains

In case you've bought several domains with multiple TLDs, OR used a subdomain to develop a new website and forgot to block it from being indexed by the search engines. Either way, you have to set up redirection to the root domain:

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

That way, all the (sub-)domains like www.site.org, www.site.net, test.site.com, will redirect to www.site.com

Deleting multiple slashes/hyphens from the URLs

Sometimes, user can accidentally type in multiple slashes, e.g. www.site.com/catalog////page-1.html. In this case, you have to set up a 301 redirect to a page with a single slash www.site.com/catalog/page-1.html:

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

In the same way, you can set up a redirect from a URL with multiple hyphens (for example, www.site.com/catalog/page---1.html) to www.site.com/catalog/page-1.html:

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

Redirecting from any URL to a lowercase URL

The search engines notice the letter case, so it's best to have your URLs in lowercase. If you haven't developed your website with this in mind, you can use this PHP-script:

$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();
}

How to move your website to a new domain? The optimal strategy for a 301 redirect

According to the most popular search engines, the best strategy for moving to a new domain is:

  • mounting a page-by-page 301 redirect from the old site to the new one;
  • don't set up a redirect from robots.txt — add the Host directive to the new domain instead.

In this case, the code on the former website will have to look something like this:

RewriteCond %{REQUEST_FILENAME} robots.txt$ [NC]
RewriteRule ^([^/]+) $1 [L]
RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
а файл robots.txt для старого сайта:
User-agent: Yandex
Disallow:
Host: newsite.com

Generating 301 redirects


If you're not particularly tech-savvy, you can use the online services for generating basic redirects:

http://www.webconfs.com/htaccess-redirect-generator.php
http://www.rapidtables.com/web/tools/redirect-generator.htm
Just enter your data and get a code for redirection between domains, directories, and URLs.

How to test the 301 redirect


With every change in 301 redirect, you need to test the site's performance:

  • whether it's working (check the main page);
  • go through the main sections and webpages of the site.

301 redirect VS Canonical — which one to use & when?


Minor details aside, in order to understand what exactly do we want to say, Google offers some clear-cut rules. In very simple terms, this is how the search engines understand our commands:

301: okay, google (or any other search engine), my page isn't there anymore and it's been permanently moved to a new URL. Please, delete the old link from the index, and pass the link juice to the new page.

Canonical: okay, google, I've got multiple versions of the same page (or its content), so, please, index only the canonical version. I will keep the other versions for people to see, but don't index them, please, and pass the link juice to the canonical page.

When is it better to use a 301 redirect?


  • This is a preferred method by default;
  • For pages, that have beenmoved permanently, or their URLs have been changed;
  • For domains, if you've moved your website to the new domain;
  • For 404 pages. For example, if a certain product has been deleted from the catalog, you can set up a redirect to a page with a similar product, or to the URL of the product category.

When is it better to use rel= «canonical»?


  • If you can't set up 301 redirects or it won't be time-efficient;
  • For duplicate content, if you want to keep both versions (for example, pages with different clothing sizes);
  • When you have multiple URLs leading to the same page (catalog categories, pages for tracking the traffic from affiliate links, etc.);
  • For cross-domain resource sharing, if you want to transfer data between pages that have different origins (domains, for example).

To sum it up

Both solutions pass the link juice and both are ranked by Google equally. 301 redirect is a bit more preferred, though.

Redirection mistakes


  • Redirection chains (avoid them to maximize the speed and the link juice flow of the website);
  • Using the wrong type of redirect (to make the right decision, you have to consider all the details);
  • Setting up internal redirects without rewriting the URLs of the links. You have to make sure that links on your website don't lead to pages with the redirect;
  • Redirecting to irrelevant pages/content. Your links should always lead either to similar pages or to the section of the site that included the requested page;
  • The wrong choice of either rel=canonical or 301 direct (see above);
  • Redirecting robots.txt (it's better to add the Host directive);
  • Any redirect, which doesn't lead to a page with a 200 status code (every link should lead to a properly working page with a 200 status response. Otherwise, don't confuse the crawlers and just show the 404 Error page).

Hopefully, this 301 how-to guide will be your cheat sheet and will help you to use a 301 redirect on your website.

If you have any questions, ask them down below. I will try my best to help you!

Read also:
Essential on-site SEO factors.
Tags:
Hubs:
Total votes 8: ↑6 and ↓2+4
Comments0

Articles