As a website owner you will want to add 301 redirects when you have a page or product that has been discontinued.  If you don't do this, your users will likely see a "404 - Page not found" message.  This will be frustrating and they'll quickly click back and leave you site, unlikely to purchase anything from you. But don't worry, 301 redirects are the answer...

What are 301 redirects?

301 redirects are a way of telling a users browser that a page has moved and automatically sending that user to a new page. 

This redirect happens quickly without the user having to take action.  Most of the time, the user doesn't even know they have been redirected to a new page, and instead of seeing an annoying "Page not found" message, they are sent to a relevant page.

Search engines like Google and Bing will also follow 301 redirects, which is important for helping them to understand and index a websites' pages.

Adding 301 redirects

As a website owner you will want to add 301 redirects when the time comes.  Imagine you have a product that has been discontinued.  You'll still have products that you want to sell, so you can redirect your users there.  If you don't do this, then most likely they will follow a link to your old product and see a "404 - Page not found" message.  This will be frustrating and they'll quickly click back and leave you site, unlikely to purchase anything from you!

Below we deal with some common redirect uses in Apache and similar servers like Nginx and Litespeed.  Some of these won't necessarily work on your server straight away, but with some experimentation you are sure to get them working.

Adding 301 redirects in .htaccess

The most common way to add new 301 redirects is in the .htaccess file in the root of your website:

htaccess-file.jpg

You can edit the file with a text editor, or if you use cPanel, you can edit it directly in the File Manager.

Be very careful! A tiny mistake can disabled your website, so it is recommended to save and test regularly if you are working on a live website.

The following examples are to be added to the .htaccess file:

Add RewriteEngine ON

Add a line in .htaccess with the following:

RewriteEngine ON

Then add your redirects after this line.

Redirect one path to another

You can redirect a whole path to a completely different one:

RewriteRule ^fundraising-events/232-abseil$ /fundraising/fundraise-for-us/adrenaline-rush/abseil? [R=301,L]

Redirect non-www

You can redirect the non-www version of your site like this so it is only available with the domain including the www.:

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect a whole directory of pages

So, imagine you have a blog under www.yoursite.com/blog/ with lots of articles.  Then imagine you move it into the about us section (www.yoursite.com/about-us/) of your site.  You can redirect like this:

RewriteRule ^blog(.*)$ /about-us/blog$1 [L,R=301]

Or try:

RewriteRule ^/join-us/careers(/.*|)$ 
https://www.mysite.co.uk/join-us/careers? [L,R=301,NC]

Redirect pages in directory to the site root

Nice simple redirect if you have removed a directory of pages and just want to redirect back to the Home page:

RewriteRule ^(join-us/careers/). /$1 [R,L]

Redirect one domain to another

If you have another domain, this is a great way to tell Google it has been permanently redirected:

RewriteCond %{HTTP_HOST} ^www.yourotherdomain.co.uk [NC]
RewriteRule ^(.*)$ http://www.yourdomian.co.uk/$1 [L,R=301]

Redirect one domain to another and send all old urls to home page

Imagine you purchase an additional domain and want to redirect it to your site:

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www.newdomain.co.uk/ [L,R=301]

Note, you can still specify individual redirects above this “catch all” one.

Redirect a page or directory on one domain to another domain/directory

Here is a neat trick to do in one redirect - a page/directory on one domain to a different page/directory on another domain 

# 301 --- http://www.domain.co.uk/about/ => https://www.newdomain.ltd/who-we-are
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
RewriteRule ^about/$ https://www.newdomain.ltd/who-we-are? [L,R=301]

Redirect a query string

So imagine a URL has been indexed like www.yoursite.com/cID=208  It’s in Google, you’re stuck with it, so just redirect it to the homepage:

RewriteCond %{QUERY_STRING} (^|&)cID=208($|&)
RewriteRule ^$ /? [L,R=301]

Force SSL on the whole site

It's a great idea to force your sites pages to be served over the secure "https" protocol instead of "http".  This means your pages are available over secure URLs only, rather than having both types of URL available to your users and Google.

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirects with %20 in the URL

Image you need to redirect the following, it won’t work:

^about\-us/wedding\-venue/recommended\-suppliers/%20https\://www\.facebook\.com/weddingstationery$ https://www.facebook.com/weddingstationery? [L,R=301]

Instead, add quotes and an actual space:

“^about\-us/wedding\-venue/recommended\-suppliers/ https\://www\.facebook\.com/weddingstationery$” https://www.facebook.com/weddingstationery? [L,R=301]

Redirect all pages to home

This works in Litespeed:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? http://www.mysite.com/ [R=301,L]

PHP Header 301 redirects

Sometimes you'll need to do something different that depends on other actions in the site.  To do this, you can add a redirect in PHP using the header() function:

header('Location: http://www.example.com/');

To specifically make this a 301 redirect, change the function to be like this:

header("Location: /foo.php",TRUE,301);

For more guidance on how to format redirects in PHP, check out this guide.

What about 302 redirects?

You may also have heard about 302 redirects and are wondering what the difference is.  As we know a 301 redirect is a permanent one, designed to tell search engines and users that a page has moved permanently.

So what is a 302 redirect and when would we use one?  Well, according to SEMRush:

A 302 redirect is a temporary way to divert users from one page on your site to a different page.

It’s important for SEO because it allows you to send traffic to a different page while maintaining the keyword rankings and link value of the original page. 

So a 302 redirect is temporary and you can use it if you want to redirect to a special offer or some other page that is going away at some point.

301 redirect writing tool

The following tool is amazing and will come up with 301 redirects for you nine out of ten times:  
https://donatstudios.com/RewriteRule_Generator

Example 1: A simple redirect of a relative URL:

donatstudios-redirect-tool.jpg

Example 2: A redirect to a different domain and an HTML page to a "folder" (pretty URL):

donatstudios-redirect-tool-different-domain.jpg

301 redirect website addons

There's another easy way of writing these, which is to install an addon such as Affixia SEO Redirects for Concrete CMS.  This addon allows a number of options to easily make redirects within your site.

It's great for non-Web Devs who need to make redirects regularly in their site.  As you can see below there are many easy ways to set up redirects:

Image courtesy of the Concrete CMS marketplace

Testing redirects

As alreasdy mentioned, save regularly, do not add a huge number of redirects in one go!  If you do add lots at once and there is an error in just one of them it may disable your website!

The Ayima Redirect Path addon for Chrome is a very useful tool, which once installed, allows you to view what redirects are acting on a URL.  If there is a redirect, it will show the path like this:

redirect-path-addon.jpg

Avoid redirect loops!

A redirect loop is a mistake, which happens when a URL is redirect to another URL, which redirects back to the first URL.  This creates an infinite loop of redirects.

The result is an error in users web browsers which stops users seeing the page.  This will also harm your SEO so is a really bad thing!  It's quite easy to cause them if your site has a lot of existing redirects, so again you must test new redirects carefully when you add them.

Here's a great guide that helps us diagnose and debug redirect loops.

What's next?

Hope this article has been helpful to you, if you have any issues with 301 redirects then please feel free to contact us and we'll try and help out.

Join the discussion

Want to have your say on this topic? Start by posting your comment below...

Can we help?

We are a digital agency, specialising in web design, development, hosting and digital marketing. If you need help with anything, feel free to reach out...

Required
Required
Required

Keep up to date

Call us