Using 301 Redirects To Fix 404 Page Not Found Errors
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 instead. In the case of a redirect, you are telling your server to forward (or redirect) any requests made of a broken URL to a working URL instead. The way you tell your server to do this is by adjusting code on your server.
For example, let’s say we need to redirect /login (a 404 error page) to /login.html (a working page on that website). There are many ways to do this, and you should work with your web development team to find the right way to make this work on your server. Two of the more common ways of implementing a redirect are through an htaccess file and through a web.config file. Let’s take each one-by-one to see how exactly they work.
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/.) This file is located in the main directory (or root directory) of your website (make sure to view hidden files). In this file, you can put a number of statements telling the server what to do.
- 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 a server-code for “Moved Permanently”).
- Then you want to state the broken URL we want to redirect somewhere else. In our case, 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 case, 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 this URL, we do want to include the domain name (so, do include “examplesite.com”).
That is it! Putting it all together, you would have a line in your .htaccess file that looks something 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.
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. Also like an .htaccess file, you add a statement telling the server to redirect a certain URL. The redirect instructions go 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 visitng “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 inlcude 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 our case, 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. The <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.
If you need help adding redirects to your website, please contact me. I’m happy to help you fix your website’s 404 not found errors.