A quick guide to 301 redirects

On why and how you hav to use redirect and rewrite rules


301 redirects are a part of the toolbox of almost every online marketeer or web developer. It is one one of those things you absolutely have to tick if you want to avoid drops in your organic traffic. It is also a no-brainer from a user-experience point-of-view. If neglected, users that click an organic result will arrive on a 404-page resulting in a lot of frustration and lower time-on-site. This in return will result in negative approvals from search bots.

First a quick recap of the concept. Suppose you have revamped an existing website and you have altered the entire structure. You have also renamed the urls to make them more SEO compliant. What about the old urls that have been indexed by Google and appear in the SERP? If someone clicks them, you want the visitor to arrive on the revamped page since the old page simply doesn’t exist anymore.

E.g: someone who clicks this result in the SERP:

https://www.mywebsite.com/old-page/

… has to arrive on the following page:

https://www.mywebsite.com/new-page/

Or it can be that you change the location of a page in your site architecture, e.g:

https://www.mywebsite.com/any-given-page

… to: https://www.mywebsite.com/subdirectory/any-given-page

In any case you have to tell the search engines that the old url doesn’t exist anymore and that is has been replaced by a new one. By doing so you also tell them to get the old url out of their index and to index the new url instead. Basically there are two kinds of redirects:

  • 302-redirects: for temporary purposes limited in time
  • 301-redirects: for redirects that are indefinite. It is this kind that is most widely used and we will dive into

By doing so, you avoid frustration on the end-user side of the story. It also allows you to shift the SEO history and page rank value of the old url to the new url. That way, you can avoid or at least limit drops in organic rankings.

There are several ways to implement 301-redirects depending on the CMS you use or your server configuration. We will walk through some of the most common cases.

1. Use a plugin in the backend (WordPress)

If you’re not that code-savvy or if you don’t have access to the core files of your website (either by FTP or a server admin panel) plugins come to the rescue. As you’d expect: there are dozens of plugins that will do the job for you out-of-the box free or paid. You do have to keep some caveats in mind:

  • Do not use a plugin that simply redirects all broken links to the homepage. Yes, you would avoid that the visitor arrives on a 404-page. However, by doing so you neglect the very essence of a 301 redirect. That is to pass the historic SEO value of a deprecated page to a new page. If you do redirect the lot to your homepage, Google will consider these as a soft 404, as explained by John Mueller.
  • Many SEO plugins offer a redirect feature, like Yoast. However, this is very often an option for paid premium versions, not for free versions.

2. Write the rules in your .htaccess file on an Apache server

If your website is hosted on a server running Apache Web Server software, you can add rewrite rules directly in your .htaccess file. This is probably the most widespread way of working and it certainly gives you much more control. Obviously you need sufficient rights to edit the file via an FTP agent or a server admin panel. An .htaccess file is a configuration file and it is located in the root directory of your website installation. This file is at the core of your installation, so please take a back-up before you start editing. You don’t see any such file in your FTP agent? Since this is a configuration file, you often have to enable ‘show invisible files’. Usually you can find this in the ‘view’ or ‘settings’ menu.

To be able to redirect anything at all, you have to be sure that the rewrite engine is turned on in your .htaccess file:


Options +FollowSymLinks
RewriteEngine On

Write your redirect rules below the declaration above.
Now let’s walk through some of the most common scenarios:


# Redirect a single page to another single page on the same domain:
Redirect 301 /old-page https://www.my-website.com/new-page
#
#
# Redirect an entire subdirectory to any given page on the same domain:
Redirect 301 /any-given-subdirectory/ https://www.my-website.com/any-given-page/
#
#
# Redirect an entire subdirectory to another subdirectory on the same domain:
Redirect 301 /old-subdirectory/ https://www.my-website.com /new-subdirectory/
#
#

Already you notice that you just have to type the relative path of the old url and the absolute path of the location it replaces. That’s just the way it works. The examples above are all redirects on one and the same domain. It can happen you have to redirect urls from an old domain to an entire new domain. 301 redirects help you out in such cases as well:


# Redirect an entire domain to another domain:
Redirect 301 / https://www.new-website.com/
#
#
# Redirect an entire subdirectory on one domain to another domain but in the same subdirectory:
Redirect 301 /any-given-subdirectory https://www.new-website.com/any-given-subdirectory
#
#
# Redirect only the homepage of an old domain to a new domain:
RedirectMatch ^/$ https://www.new-website.com

What is the difference between Redirect, RewriteRule and RedirectMatch?

Did you notice the last rule is different from the rules above? You might wonder what the difference is between the plain Redirect and the more complex syntaxis of Redirect Match? Here’s a quick overview:

  • RewriteRule is handled by Apache’s mod_rewrite Module. It works server-side and it doesn’t change the URL in the browser address bar. It doesn’t redirect either. A good example are WordPress’ rewrite rules where you change the default urls into clean urls.
  • Redirect is handled by Apache’s mod_alias. This works client-side and it does change the url in the browser address bar. This is more useful for SEO purposes since it forces the search engine to update the url whereas RewriteRule simply hides the seo-unfriendly url.
  • RedirectMatch allows you to add regular expressions, giving you a lot more control to perform bulk operations. This can come in handy if you are dealing with an extremely complex website structure with a lot of nested subdirectories.

3. Rewrite rules on an Nginx server

If your website is hosted on an Nginx server, things are a little bit different. Nginx servers simply don’t have a .htaccess file. Website-level configurations have to be done in a server block in on your server’s admin panel. It speaks for itself you need access to this. The purpose is exactly the same, only the syntaxis differs a bit compared to Apache:


# Redirect a single page to another single page on the same domain:
rewrite ^/old-page$ /new-page permanent;
#
#
# Redirect an page in any given subdirectory to another page in another subdirectory:
rewrite ^/any-given-subdirectory/any-given-page$ /another-subdirectory/another-page permanent;

301 redirects offer you a powerful tool to keep your SEO rankings consistent. As soon as you have made the changes, you can do a quick check in your browser by typing the old url. If everything goes well, you should be redirected to the new url immediately. Search engines on the other hand need some more time to update their indexes. It is best to leave the redirect rules where they are for at least a couple of months. But if your tests go to your satisfaction, you can rest assured the bots will follow suit.

Posted on May 15, 2019 by , last modified on May 15, 2019

Copyright © 2024 - All rights reserved - sitemap - disclaimer - privacy policy - cookies