Redirecting URLs: How To Use & Implement 301 Redirects
By Matthew Edgar · Last Updated: July 21, 2023
A redirect routes visitors from one URL to another URL. Redirects can be useful to fix 404 errors or to prevent errors when changing URLs. However, it is important you configure redirects to send visitors to the correct URL. Improperly configured redirects can result in other errors, like redirect chains or loops, which will be discussed later in this guide. In this article, we’ll cover all aspects of working with redirects, including implementing 301 redirects.
Table of Contents
- What Is a Redirect?
- Common Redirect Questions
- Why use a 302 temporary redirect?
- What are 307 or 308 Redirects?
- What are redirect chains?
- What are redirect loops?
- How do you clean up redirect chains and loops with ChatGPT?
- How do redirects impact traffic?
- How long should I keep redirects?
- How do you use 301 Redirects to fix 404 Errors?
- How do you use redirects to avoid 404 Errors during a redevelopment?
- What page should you redirect to?
- How To Implement Redirects
- Testing Redirects
What is a Redirect?
A redirect is a piece of code instructing the server to forward (or redirect) any requests made at one URL to a different URL. The URL you are redirecting from is called the redirect source (or redirect origin) and the URL you are redirecting to is called the redirect destination (or redirect target).
Once a redirect is implemented, any visitor or robot attempting to access the redirect source URL will be routed to the redirect destination URL. When the server processes this redirect, an HTTP response status code is returned to the browser to indicate that a redirect has occurred and clarify what type of redirect it is. The most common redirect status code is a 301 response, which indicates this is a permanent redirect. A 302 response status code would indicate the redirect is temporary. (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.)
Common Redirect Questions
Why use a 302 temporary redirect?
If the redirect is truly temporary, then a 302 response code on the redirect is the right solution. For example, if you are temporarily shutting down a promotion page on an e-commerce website but will re-open that promotion page in a few days, a 302 would be appropriate. A 302 can also be appropriate when redirecting to new URLs if there is a chance of reverting to old URLs during a limited test phase.
What are 307 and 308 redirects?
A 307 or 308 response status are 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 treats a redirect returning a 308 status like a 301 redirect. As both status codes are supported, you do not need to change your current 301 redirects to a different status code.
A 307 response code on a redirect is most often seen within testing tools, including Google Chrome, when testing redirects between non-secure and secure websites. The 307 response status is returned in the browser even though the server may not be configured to return a 307 redirect. That’s because browsers are redirecting to https using an internal redirect and internal redirects return a 307 status. Internal redirects are not seen by Googlebot because this type of redirect isn’t a “real” redirect returned from the server. If you see a 307 response status in the browser, you’ll need to check what status code the server is actually returning as that will be the status code seen by robots (including Googlebot).
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, the redirect source is asite.com/A and the final redirect destination is asite.com/D. However, the redirect takes three hops to arrive at that destination, 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 destination of the redirect chain. Chains can also lead to slower speeds (learn more about how redirects slow down a website).
As much as possible, chains should be avoided. In the above example, the redirects from asite.com/A and asite.com/B should both be updated to redirect to asite.com/D in one hop. If a chain cannot be avoided completely, chains should be reduced to as few hops as possible.
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 redirects in a loop; visitors will be unable to access any pages within the redirect loop. Robots will waste crawl budgets when encountering loops. Human visitors and robots will both encounter error messages due to redirect loops.
Here is an example of a redirect loop. This starts at asite.com/A and redirects through a chain back to asite.com/A.
asite.com/A -> asite.com/B
asite.com/B -> asite.com/C
asite.com/C -> asite.com/A
How do you clean up redirect chains and loops with ChatGPT?
Instead of addressing chains and loops manually, you can use ChatGPT to clean up redirect chains and loops on your website.
- The first step is exporting your existing redirects into an Excel spreadsheet.
If you are using Redirection in WordPress, you can select “Import/Export” to download the CSV file. You want to download “Everything” under Export options. The CSV can be opened in Excel.
If your redirects are defined on your website’s .htaccess file, you can open the .htaccess file in Excel. On the import wizard screen, you can select “space delimited” and then the redirects will be added as new columns.
- Next, you want to clean up your Excel file so that your redirect source is in column A and your redirect destination is in column B. For the purposes of cleaning up the chains and loops, you can remove the data in the other columns.
- You could, at this point, review the redirects manually to remove any loops or chains. Using ChatGPT, however, tends to be far more efficient when you have many redirects to check for loops or chains. The prompt to use for ChatGPT is:
“Chat, below is a list of redirect sources and destinations, where the source is listed first and the destination second. Please check if there are any redirect chains or redirect loops within these redirect statements. If loops or chains are found, please rewrite the rules to avoid those issues and return the results as a table.”
After this prompt, copy and paste the data in the Excel file below the prompt text. Depending on the size of your redirects, you may need to paste this in multiple batches. If that is the case, tell ChatGPT you will be pasting in multiple batches and to wait to clean up the redirects until you tell it you’ve pasted the final batch.
- ChatGPT will then process the redirects. If any redirect chains or loops are found, ChatGPT will return a table with the corrected redirects. If no issues are found, it will tell you.
- You can ask ChatGPT to format the updated redirect rules into a CSV file that you can upload to Redirection in WordPress. Or, you can ask ChatGPT to format the updated redirect rules into an .htaccess file that you can upload.
Remember! Before uploading the redirects, check that the updated redirect rules are correct. ChatGPT is helpful but not flawless.
How do 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.
How do you use 301 Redirects to fix 404 Errors?
One way to fix not-found errors on your website is by adding redirects. 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 taken to /login.html and avoid the redirect.
You can also add preventative redirects if there is a good chance a URL might be typed incorrectly. This is especially important for URLs used in offline advertising. For example, a brochure might tell people to visit yoursite.com/sale. Some people might forget the “e” at the end of the word, so it would make sense to redirect yoursite.com/sal to yoursite.com/sale before the brochure is distributed so that all customers can access the page.
How do you use redirects to avoid 404 Errors during a redevelopment?
When you are updating your website, as part of a redesign, redevelopment, or some other type of major overhaul, you will occasionally need to change the URL of a few pages. 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 occurring.
In some cases, every URL (or almost every URL) will change as part of the redesign or redevelopment. One of the most common reasons for this 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 need for redirects and lessens the chances of losing traffic with a redesign.
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.
What page should you redirect to?
Deciding where some URLs should redirect to is easy, especially for updated pages. For example, a page about an old product might have been removed from the website, but there is a new version of that product. In this example, the URL of the removed page should redirect to the URL of the new page.
What about pages that do not have a new version? In these cases, the removed page’s URL should redirect visitors to a page that meets similar expectations as the removed page. For example, an old product page was removed and there is no new version of the product, but a redirect could route visitors to a category page that lists products similar to the product 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 broadly focused, so the chance the home page will be similar enough to the removed page visitors wanted to find is unlikely.
If there is no relevant page to redirect to, it might be better to not add a redirect at all and, instead, let the removed page return a not-found error. 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 an irrelevant page.
How To Implement Redirects
There are two different ways to implement a redirect: in client-side code or in server-side code.
- Server-side redirects. Defined in server configuration files, these redirects are processed on the server (Apache, Nginx, Windows, etc.) before any content is sent to the browser. The server sends an HTTP response with the appropriate status code (e.g., 301, 302, 307) and the URL the browser should redirect to in the ‘Location’ header.
Server-side redirects are generally the fastest method because they minimize the number of HTTP requests. Server-side redirects also avoid loading and processing the initial page into the browser. Simpler is better, especially when it comes to search robots. As a result, search robots are able to reliably and consistently follow server-side redirects. Users are also typically unaware that a redirect has occurred because of how quickly server-side redirects are processed.
A server-side redirect is a redirect processed by a web server. These types of redirects can be configured in a number of different ways. Most often server-side redirects are configured via the .htaccess file (on Apache – see below) or via a web.config file (on IIS/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 the server returns a 301 response status code to indicate the redirect is permanent. A 302 HTTP response code, which indicates the redirect is temporary, is often the default setting on most servers.
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 BuiltWith.) 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 redirect the URL, so the redirect type is 301.
- Then you want to state the URL of the redirect source. To use the example above, let’s say we are fixing the broken link of “/login”. So, the redirect source URL to state in the .htaccess file code is /login. Note that, this URL shouldn’t include your website’s domain (this is called a relative URL).
- Finally, you want to state the redirect destination. In the example above, “/login.html” is the working version of the page and therefore, this is the URL we want to redirect to. We’ll state this by adding http://www.examplesite.com/login.html. For the redirect destination URL, it is best to include the domain name to make the redirect clearly stated (this is called an absolute URL).
Putting it all together, the line in your .htaccess file looks like this:
redirect 301 /login https://www.examplesite.com/login.html
After saving the .htaccess file to your server, the next time you visit examplesite.com/login, you will be redirected to examplesite.com/login.html.
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 redirect source or origin), and the “Target URL” is the URL you are redirecting to (the redirect target or destination). The Redirection plugin also allows you to handle redirects with regular expressions and also lets you group redirects for better organization. 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.
Method #1: window.location.href
redirect_to_url = "https://site.com/redirect-destination";
window.location.href = redirect_to_url;
There is a way to do something similar with the code: window.location.assign(redirect_to_url); The difference is semantic. When you use window.location.assign(redirect_to_url), you’re specifically invoking a method to load a new document. When you use window.location.href = “redirect_to_url”, you’re changing the property of the window.location object, which implicitly loads a new document.
Method #2: window.location.replace()
redirect_to_url = "https://site.com/redirect-destination";
The window.location.replace() method is the most similar to a server-side redirect and is typically the best method to use for client-side redirects. If you use this method, the current page will not be kept in the browser’s history. If the user clicks the “back” button on their browser, they will not return to the original page but instead to whatever page was in the history before the original page.
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 see a note that this URL is a “Page with redirect”, which is what we’d expect to see for a correctly implemented redirect. Note that a page with a redirect will not be indexed. Learn more about Google’s Page Indexing reports and why these pages are not indexed.
Other Methods of Testing Redirects
Another way of testing a redirect is to use a tool like WhereGoes. In WhereGoes, 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 the /noindex-vs-nofollow-vs-disallow/ article.
Get Help With Your Redirects
Want help with your website’s redirects or have more questions about redirects? Contact me today to discuss how 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.