How to set up 301 redirects

Right decorative Article tab

Jan 8th 2020

301 redirection is the process of permanently diverting an old URL to a new one. As well as aiding user experience, 301 redirects are essential for passing the rankings and SEO value of the old page to the new one, so it’s important that they are done correctly.

Because it is technically more difficult to set up 301 redirects, many website owners and managers opt for using 302 temporary redirects, which isn’t a good idea in the majority of cases. Not sure why? Find out the difference between a 301 permanent HTML redirect and other forms of redirect.

There are various ways to implement a 301 redirect, depending on the situation… whether you are redirecting a www. to a non www. (and vice versa), whether you are retaining the filename or not, whether you are using Apache mod_rewrite or not, and much more.

We’ve detailed below how to implement 301 redirects in Apache and Nginx. For further information check out Moz’s redirection guidelines.

301 Redirects in Apache

In most cases, a web server running the Apache Web Server software’s behaviour is configured using an .htaccess file. An .htaccess file is a simple text file that sits in the root directory of a website site and it is used to to enable and/or disable Apache’s features and functions, including redirect functionality.

In order to implement a 301 redirect the following code will need to be added directly to the sites .htaccess file...

redirect 301 /old.html http://www.redirectlyapp.com/new.html”

  • The “redirect 301” tells search engines (and browsers) that the page has been moved permanently
  • The “/old.html” provides the original location of the page
  • The “http://www.domain.com/new.html” is the new URL of the page and is where the server should redirect visitors to. Please note: this part of the code requires the URL to be entered in its entirety.

It is important to note that if you are moving multiple pages, you will need to include a redirect statement for every URL you’re moving. Rather than doing this line by line you can use the Apache module mod_rewrite (which is installed by default on most Apache installations) to match specific patterns for entire folders and redirect them to their new URLs without having to go through every PHP script.

The mod_rewrite module operates in per-server context or in per-directory context. The per-server context requires that a developer must edit the apache configuration file, httpd.conf at the very top level. The per-directory context uses .htaccess files that exist in each folder a user wants to configure so different sites on the same server can have different configurations.

301 Redirects in Nginx

In Nginx, you can accomplish most redirects with the built-in rewrite directive. This directive is available by default on a fresh Nginx installation and can be used to create both temporary and permanent redirects. In its simplest form, it takes at least two arguments: the old URL and the new URL.

You can create a permanent redirect in Nginx by adding a line like this to the server block entry in the server configuration file:

rewrite ^/old.html$ http://www.redirectlyapp.com/new.html permanent;”

  • The “rewrite” matches regular expression patterns in parenthesis.
  • The “/old.html” provides the original location of the page
  • The “http://www.domain.com/new.html” is the new URL of the page and is where the server should redirect visitors to.
  • The “permanent” tells Nginx that this is a permanent redirect so it’ll use the 301 (Moved permanently) response code. Alternatively you could use “redirect” instead of “permanent” and it would use the 302 (Found) response code.

There are some useful tools you can use to help ease the pain and save time when carrying out 301 redirect strategy. Here’s a list of 301 redirect tools and integrations the work alongside the world's most widely used content management systems.

Let’s get started

Save time. Be one of the first to use Redirectly.

Join our waiting list to get early access to our BETA and immediate access to our demo playground.

Please correct the following error(s):

  • ${ error }
Thank you for joining the waiting list for access to Redirectly’s BETA.

We’re working hard to put Redirectly into the hands of developers and SEO strategists all over the world and will have you on board as soon as we can. We'll be in touch soon!

In the meantime please feel free to visit our demo playground. Visit redirectlyapp.com/play to access.