Many years ago when I first started tinkering with websites and dabbling in the world of SEO, I learned about the importance of 301 redirects. Conceptually, a 301 redirect is very straight forward. Allow me to explain…
What Is A 301 Redirect
In a simple sense, a 301 redirect is the method at which you tell browsers and search engines that a URL has been permanently moved to a new location (URL). For example, you had a page on your website that used to live at domain.com/awesome-page/ and it is now located on domain.com/awesome-new-page/. The preferred way of communicating this is by using a 301 redirect. That is great! But, what does it really mean?
Every time you enter a destination URL in your browser, the browser will send a request to your web server. The web server will then return one of many HTTP status codes that indicate the state of the web page you we are looking to visit. The most common HTTP status code is 200, which means the web server successfully returned the requested page. If you implemented a 301 redirect on your website, the server will return a 301 status code indicating that the requested URL has permanently moved.
Google explains a 301 as such, “The requested page has been permanently moved to a new location. When the server returns this response (as a response to a GET or HEAD request), it automatically forwards the requestor to the new location.”
Different Types of 301 Redirects
There are a variety of ways you can write a 301 redirect and if you are using an Apache server, they are all fairly simple. If you are using a Windows server, they are much more complicated. I’m not going to go into the specifics behind writing 301 redirects on Windows, but I will recommend you read this tutorial if you find yourself in such a situation.
The common methods used to write 301 redirects on Apache servers are as follows:
- Server side redirects
- Redirects using RedirectMatch (and Regex)
- Mod_rewrite redirects
- Single file (URL) redirects
Server Side Redirects
Server side redirects are a thing of the past (in my opinion). They may be the only or best option in certain instances. Examples of server side redirects include writing your redirect in PHP, ASP, ASP .NET, or Java. These would be placed in the “header” of each specific page where you would like the 301 redirect to occur.
RedirectMatch and Regular Expressions (Regex)
Using a RedirectMatch for 301 redirects is generally considered a “stronger” command compared to other types of 301 redirects. A RedirectMatch is implemented in the .htaccess file of your website and can be very complex, especially when using regular expressions. Regular expressions will allow you to create formulas that will trigger the 301 redirect to occur when specific criteria are matched. Here is an amazing explanation of regular expressions and the different ways you can use them in .htaccess.
For those of you who aren’t familiar with .htaccess files, they are basically a file that exists at the root directory of your website which enable you to configure your web server with various commands (e.g. 301 redirects, blocking user IP addresses, etc.). You can learn more about them here.
Mod_rewrite Redirects
Utilizing a mod_rewrite command to handle your 301 directs is another option that many SEO professionals use when mapping old pages to their new locations. Mod_rewrites also utilize regular expressions to be able to use a single command line to be able to redirect a batch of pages. This is obviously preferred over doing 100 single line redirects.
Single File (URL) Redirects (MOST COMMON)
A single file redirect is the most common type of 301 redirect in my experience. Usually, you will only need to redirect select pages that have changed on your website. These are just a single line of code that is placed in your .htaccess file. This will be explained in greater detail in the sections following.
How to Write a 301 Redirect
As mentioned in the title, the purpose of this post is to explain how to write a 301 redirect and provide you with a Google Spreadsheet template to make mapping your pages easy.
Let’s take a look at a simple 301 redirect example. You had a page on your website that used to exist at http://yourdomain.com/page/, but it is now located at http://yourdomain.com/new-page/. In order to pass the link juice appropriately and comply with Google best practices for SEO, you want to make sure you redirect this page with a 301 redirect.
To do this, you would use a single file redirect that is placed in your .htaccess file (assuming you are using an Apache server). It would look like this:
Redirect 301 /page/ http://yourdomain.com/new-page/
This line of code is telling Google and browsers that your old page located at “/page/” (you ALWAYS drop the root domain from the rule) has been permanently moved to “http://yourdomain.com/new-page/” (you ALWAYS add the root to this part of the rule).
How to Access your .Htaccess File
The .htaccess file for your website is always found in the root folder associated with your domain. In most cases, the file will already exist as the result of your host. However, if the file does’t exist you can easily contact your hosting provide for assistance, or download a WordPress plugin like WordPress SEO which will assist in adding an .htaccess file to your website.
Adding a 301 Redirect to your .Htaccess File
Adding a 301 redirect to your .htaccess file is very simple. You literally, copy the line of code (like the example above) and paste it in your file. It is a good idea to utilize comments to know what each redirect is used for. This can be done by placing a # before your notes. An example of a 301 redirect and comment in your .htaccess file would look something like this:
# Custom redirects
Redirect 301 /page/ http://yourdomain.com/new-page/
Please note: I’d recommend adding your redirects at the bottom of the file because in some cases, depending on the rules that exist within your .htaccess file, the order of which the rules are placed will matter. It may cause some rules not to work.
Using A Google Spreadsheet Template to Write 301 Redirects
The beauty of writing single page 301 redirects is that they all follow a similar pattern across an entire domain. Meaning, every 301 redirect that you write for the same domain will fit in the same formula. For this reason, I created a Google Spreadsheet that will help you accomplish this task.
You can find the Google Spreadsheet template for 301 redirects here.
How to Use this Google Spreadsheet
The Spreadsheet is pretty straight forward, but you can follow these steps to get started:
Step 1: Open the Spreadsheet
Click the above link and a new tab will open in your browser with the spreadsheet.
Step 2: Copy the Spreadsheet
Once the Spreadsheet is open, click File and select Make A Copy from the drop down options.
Step 3: Rename the Spreadsheet
Now that you made a copy, rename this whatever you’d like it to be called so you can find it later.
Step 4: Adjust the Spreadsheet Formula
In order for the Spreadsheet to work well with your specific domain, you will need to make an adjustment to the formula. You can do this by selecting cell D3. In the fx bar, you will see the formula that is CONCATENATING all the cells and looks exactly like this:
=CONCATENATE(A3&” “&(RIGHT(B3, LEN(B3)-24))&” “&C3)
You will want to adjust the number 24 so it works for the length of your root domain because as I’m sure you will remember, you want to remove the root domain from the first part of the 301 redirect rule. In the example, the root domain is “http://exampledomain.com/” so the number is set to 24 so it will remove the root domain from the formula. If your root domain was 22 characters long, you will want to make this number 22 so it looks like this:
=CONCATENATE(A3&” “&(RIGHT(B3, LEN(B3)-22))&” “&C3)
Step 5: Add your 301 Redirect to the Spreadsheet
In the Spreadsheet, you will notice 4 columns, that are easily labeled for you. Below are slightly longer explanations of each:
- Redirect Rule- This shouldn’t be changed and should remain as “Redirect 301”
- Page with 404 Error- This is the page that no longer exists and the one you need to 301 redirect
- URL Where You Want 404 Redirected- This the new page that you want the old page redirected to
- Paste In .Htaccess file (or plugin)- This is the line of code that you will paste in your .htaccess file as outlined in the above section
Common Pitfalls with 301 Redirects
In general, 301 redirects are very straight forward. However, be sure to test that your redirects work properly before concluding that you completed the task. If you have a bunch of redirect rules in the same .htaccess file, make sure the previous rules still work when adding new rules. Also, make sure that you don’t redirect a redirect because this will result in an infinite loop which means the user won’t be able to reach the desired page because they are just circling around the interweb.
Helpful Resources
There are a bunch of resources available to help you when rewriting 301 redirects. Below is a list of my favorite ones:
Built With
This will help you identify which type of server you are running in the event you have no idea.
Htaccess Testing Tool
This will let you test your 301 redirects to make sure they will work properly.