Redirecting URLs: How To Use & Implement 301 Redirects
A redirect routes visitors from one URL to another URL. Redirects can be useful to fix 404 errors or to help avoid errors when changing URLs but it is important you redirect to the right place. In this guide, we’ll cover all aspects of working with redirects, including implementing 301 redirects.
Table of Contents
- What Is a Redirect?
- Why Use Redirects?
- What Page Should You Redirect To?
- How Do You Implement a Redirect?
- Testing Redirects
- Other Redirect Questions
Don’t want to read? Check out this video which provides an in-depth guide of how to use redirects.
What is a Redirect?
You can think of a redirect like the online version of mail forwarding. If you move, you tell the post office to forward mail arriving at your old address to your new address. In the case of a redirect on a website, you use a redirect to tell your server to forward (or redirect) any requests made to one URL to a different URL. The URL you are redirecting from is called the redirect source and the URL you are redirecting to is called the redirect destination.
Once implemented, anybody who attempts to access the redirect source will be routed to the redirect destination. When the server processes this redirect, a response status code is returned to the browser to indicate what type of redirect it is. The most common redirect response status code is a 301, which indicates this is a permanent redirect. You can also use a 302 response status code to indicate the redirect is temporary.
Why Use Redirects?
Use 301 Redirects to Fix 404 Errors
Inevitably, you are going to have not-found errors on your website. People type words in wrong, you change URLs and forgot to add a preventative redirect, or you remove old pages from your website. Let’s say, for example, that /login is a 404 not-found error on your website. To fix that error, and prevent people from encountering the error, you can add a redirect /login (the 404-error page) to /login.html (a working page on that website). That way, if anybody attempts to access /login, they will instead be taking to /login.html and avoid the redirect.
Use Redirects to Avoid 404 Errors
When you are updating your website, as part of a major overhaul, consolidation of pages, or routine maintenance, you will occasionally need to change the URL of a particular page. When you change the URL, anybody attempting to access the old URL will receive an error message saying that URL can no longer be found. Instead, you want to redirect visitors and robots who attempt to access the old URL to the new version of the URL to prevent a 404 error from ever occuring.
What Page Should You Redirect To?
Redirecting updated pages is easy. For example, a page about an old product might have been removed from the website, but there is a new version of that product. So, the fix is to redirect the URL of the removed page to the URL of the new page.
For removed pages, the redirect should take visitors to a page that meets similar expectations as the removed page. For example, an old product page was removed but there is no new version of the product, so a redirect could route visitors to a category page that lists products like the one that was removed.
It is important to avoid redirecting visitors to a page that isn’t something people wouldn’t expect as this is frustrating and confusing. Let’s say, a visitor is trying to find a product you no longer carry but that old URL redirects me to your website’s about page—the about page has nothing to do with the product the visitor was interested in. One common example of this is the home page. The home page is almost always broadly focused, so the chance the home page will be similar enough to the removed page visitors wanted to find is unlikely.
In short: find the most relevant page you can to redirect to. If no relevant page exists to redirect to, it is okay to let people see an error message. When reaching an error message, especially a well-crafted error message, visitors know the page they were hoping to access is no longer available instead of being confused after being redirected to some random and irrelevant page.
One of the most common reasons for updating URLs, is when you are moving your website to a new platform. The platforms may generate different URLs (e.g. page.aspx instead of page.php) or the new platform might force a directory structure (e.g. all blog posts must be in a /blog/ directory). This is an area to evaluate when selecting new platforms; a platform that requires no (or only a few) changes to URL structure is preferable since it reduces the needs for redirects and lessens the chances of losing traffic.
When transitioning a website with only a few pages with changed URLs, redirecting each page is easy. But if you have lots of pages with changed URLs (for example, thousands of product pages all have changed URLs) sometimes it is more practical to only select the “best” pages to redirect. This may include redirecting pages with several external links, high traffic volume, a high number of social shares, a higher conversion rate, or some combination of those factors.
How Do You Implement a Redirect?
There are different ways to process the redirect, but the most reliable method is handling the redirect via the server. When a user accesses an old URL, the server intercepts that user and sends that user to the new URL. The user is largely unaware that the redirection occurred.
A server-side redirect can be configured a number of different ways. Most often server-side redirects are configured via the .htaccess file (on Apache), via IIS (Windows), or via a web.config file (also Windows). There are also several other ways to handle redirects in a variety of programming languages. When configuring a server-side redirect, you want to make sure to use a 301 response status code to indicate the redirect is permanent. This will help Google more quickly refresh their search index, making sure that searchers find the right pages on your website. A 302 HTTP response code, which is often the default setting and the easiest to implement, indicates the redirect is temporary and, as a result, the redirect may not be updated as quickly by Google in search results. (Side note: there is some anecdotal evidence that Google might treat 302 redirects as 301 redirects in some cases but, even still, a 301 is a clearer signal to send.)
htaccess 301 Redirect
An .htaccess file is a control file for Apache servers. (You can find out what kind of server you are on by using a tool like http://builtwith.com/.) The .htaccess file is located in the main directory (or root directory) of your website (make sure to view hidden files). In the .htaccess file, you can put a number of statements telling the server what to do, including statements about redirects. Here are the steps to add a 301 redirect to the .htaccess file:
- To create the statement in the .htaccess file, you want to first state that you are making a redirect with the statement redirect.
- Next, you want to define the type of redirect. In this case, we want to permanently fix the broken link, so the redirect type is 301 (which is the HTTP response status code for “Moved Permanently”).
- Then you want to state the broken URL we want to redirect somewhere else. To use the example above, let’s say we are fixing the broken link of “/login”. So, we’ll want to state /login. Note that, this broken link shouldn’t include your website’s domain (so, no “examplesite.com”).
- Finally, you want to state what URL to redirect to. In our example above, this is “/login.html”, which is the working page on this website. We’ll state this by adding http://www.examplesite.com/login.html. For the URL you are redirecting to, we do want to include the domain name (so, do include “examplesite.com”).
That’s it! Putting it all together, you would have a line in your .htaccess file that looks like this:
redirect 301 /login http://www.examplesite.com/login.html
After saving your .htaccess file to your server, the next time you visit examplesite.com/login, you will be redirected to examplesite.com/login.html.
web.config 301 Redirect
If you don’t have an Apache server, adding in the redirect via an .htaccess file won’t work. Another common method is adding in a redirect via a web.config file. Like the .htaccess file, a web.config is located in the main directory (or root directory) of your website. Like an .htaccess file, you add a statement telling the server to redirect a certain URL. The redirect statement goes in between the
<configuration> </configuration> tags.
- To create the statement in the web.config file, you will start by defining the path of the file you want to redirect (the broken link). For this example, let’s say that “http://www.examplesite.com/calendar/634.html” returns a 404 error. To prevent people from visiting “calendar/634.html” and receiving that error message, we want to redirect that URL to a working URL. To do that in the web.config file, we need to state our redirect path as:
<location path="calendar/634.html">Note that we don’t include examplesite.com (our domain) in this path definition.
- Next, we want to define our destination, which is where we want the broken link to redirect to. In this example, we want to redirect the broken link to “http://www.examplesite.com/calendar.html”. This instruction is contained within an attribute of the httpRedirect tag. The attribute name is destination and the value is the full URL (including the domain examplesite.com) we want to redirect to. That statement looks something like
<httpRedirect enabled="true" destination="http://www.examplesite.com/calendar.html"/>
- In the httpRedirect tag, we also want to add an attribute httpResponseStatus and set that attribute to “Permanent”. This indicates that this is a permanent solution to the broken link. The full httpRedirect tag statement looks like
<httpRedirect enabled="true" destination="http://www.examplesite.com/calendar.html" httpResponseStatus="Permanent"/>
- Finally, we want to contain the httpRedirect tag within a
<system.webServer>tag ends up contained within the
<location path="calendar/634.html">tag we defined in step 1.
Putting all of that together, we end up with a full statement that looks like this:
<httpRedirect enabled="true" destination="http://www.examplesite.com/calendar.html" httpResponseStatus="Permanent" />
After adding this to the web.config file, going to “http://www.examplesite.com/calendar/634.html” will redirect us to “http://www.examplesite.com/calendar.html”.
Browser-Based (Client-Side) Redirects
For WordPress (and other CMS systems), there are plugins that will help with configuring and managing redirects. The plugin I’ve found works the best for clients and that I highly recommend using is called, simply, Redirection. Once installed, Redirection can be located under the “Tools” menu. From the main page, you can edit existing redirects or you can add a new redirect.
On the add new redirect screen, the “Source URL” is the URL you are redirecting from (the old address) and the “Target URL” is the URL you are redirect to (the new address). Redirection also allows you to handle redirects with regular expressions and also lets you group redirects to stay organized. By default, Redirection will set the redirect as a 301 but if you want to change that, you can click the gear icon for more options and set the redirect to a different response code.
Google Search Console
To check that a URL redirects in Google Search Console, you can use the URL Inspection tool by entering a URL in the search box at the top of the screen.
Google Search Console will then return the results indicating what they know about the URL. If Google’s bots have previously crawled the URL, you will likely see something like this. The “Coverage” section notes that this is a “Page with redirect”, which is what we’d expect to see for a correctly implemented redirect.
If the redirect has recently been added, you can ask Google to test the live URL by clicking the “Test Live URL” in the upper right corner. Once the live test has run, in the “Availability” section you will see the “User-declared canonical” and the URL listed there should be the URL you are redirecting to. If it is, then Google has detected the redirect.
Other Methods of Testing Redirects
Another way of testing a redirect is to use a tool like Where Goes. In Where Goes, you can enter a URL, confirm that the proper response is returned when it is redirected, and confirm the URL redirects to the right location. Begin by entering the URL on Where Goes’s main screen and then click “Trace URL”.
The result will then be returned. In this example you can see that /tech-seo/controlling-search-robots/ on my website redirects with a 301 response to /noindex-vs-nofollow-vs-disallow.
Other Redirect Questions
Would I ever want to use a 302 redirect?
Yes! If the redirect is truly temporary, then a 302 is the right solution. For example, if you are temporarily shutting down a promotion page on an ecommerce website but will re-open that promotion page in a few hours, a 302 would be appropriate. Google also recommends returning a 302 for mobile dedicated websites (if you are still using that configuration).
What are 307 or 308 redirects?
A 307 or 308 response status are new status codes available in HTTP 1.1. A 307 response indicates a temporary redirect and a 308 indicates a permanent redirect. The 308 code is supported by Google and will be treated like a 301 redirect. As you are likely using permanent redirects, you could use a 308 instead of a 301. However, both status codes are supported so you do not need to change your current 301 redirects to a different status code.
The 307 redirect is a bit trickier to explain. You’ll most often see this within testing tools, including within Google Chrome, when testing redirects between non-secure and secure websites. The catch is the 307 is returned in the browser even though the server isn’t configured to return a 307 redirect. That’s because browsers may send a 307 redirect when redirecting to https using an internal redirect. These types of 307 redirects are not seen by Googlebot because this type of redirect isn’t a “real” redirect. My recommendation is not to use the 307 status on your redirects and, instead, make sure your server returns a proper, permanent redirect when redirecting to a secure connection.
What are Redirect Chains?
A redirect chain is where several URLs redirect from one to the next to the next. Each step in the chain is a redirect hop. Example:
asite.com/A -> asite.com/B
asite.com/B -> asite.com/C
asite.com/C -> asite.com/D
In this example, asite.com/A redirects to asite.com/D in three hops, creating a redirect chain. Note that the URL asite.com/B redirects to asite.com/D in two hops.
Robots waste resources crawling through these redirect chains and may simply stop following the chain after a certain number of hops, meaning the robots may not locate the final page in the redirect chain. Chains should be avoided at all costs. In the above example, asite.com/A, asite.com/B, and asite.com/C should redirect to asite.com/D in one hop.
What are Redirect Loops?
A redirect chain that circles back onto itself is called a redirect loop. No destination can be arrived at by following the redirects, meaning visitors will be unable to access any pages within the redirect loop. Robots will waste crawl budgets and human visitors will see an error message in their browser.
Example of a redirect chain:
asite.com/A -> asite.com/B
asite.com/B -> asite.com/C
asite.com/C -> asite.com/A
How Will Redirects Impact Traffic?
Even when adding a redirect correctly, you will often see a dip in traffic from Google and potentially a dip in rankings in Google search results as Google learns about the redirect and the new URL. This can be especially problematic during a website redesign, though redirects can be a key part of recovery.
How Long Should I Keep Redirects?
The length of time for Google to adjust will vary with some reports saying Google recognized redirects within a matter of hours and other reports suggesting it can take months. For more, see my article about how long to keep redirects.
Get Help With Your Redirects
Want help with your website’s redirects or have more questions about redirects? Contact me today to discuss how the Elementive team and I can help. Or, if you prefer a more DIY approach, order my book, Tech SEO Guide, a reference guide to help you address redirects and other technical SEO issues affecting your website.