When you’ve been running a website for a long time, files have a tendency to move. Perhaps you’ve updated the software running the site, or decided to refile things in a more logical manner. These types of tasks are part of website maintenance and development. However, the important thing is to understand how you should deal with these moved files.
Once your website has been running a while it’s probably picked up a fair amount of traffic from the various search engines crawling and cataloging the files on the site. These search engines put great weight on being able to both access your website and find your files. If you move or delete files then the search engines will attempt to crawl your site and be met with a load of 404 “File not found” errors. This will lose your page ranking for those files as well as potentially hinder the general ranking for your site on the search engines. This is not ideal.
Fortunately, there’s a solution to this problem in the form of the 301 redirect. This makes use of the HTTP status code 301 to inform a web browser that a file has been permanently moved. By configuring a 301 redirect you are informing web browsers, and importantly search engine spiders, that the file has moved and where it has moved to. This tells the search engine spiders to update the location where they are looking for the file and means that the file keeps its previous page ranking with the search engine. It also means users browsing the site aren’t met with 404 errors but the files that they are looking for.
Let’s look at how we can configure these redirects using the Apache web server. There are a few ways to do it depending on the environment that you are in:
First, there’s directory level redirection based on an .htaccess file. For those who aren’t aware, the .htaccess file is a file that you can create in a directory on an Apache web server. The file will provide additional instructions for Apache as to how it should be handling files in the directory. This is a system provided for users in a shared hosting environment allowing users to modify Apache’s behavior for their site when they have no access to the core Apache configuration.
The other method, which is recommended if you are the server administrator or have administration access over the Apache configuration, is to add the redirect to the VirtualHost configuration section of your Apache configuration for the site requiring the redirect. This helps minimize the number of places that you may have configuration rules to check if something goes wrong, as well as improve performance. This is because each additional .htaccess file slows Apache down as it scans for and reads the file.
Regardless of which method you use for setting the 301 redirect, the syntax that you would use in the file for it is the same:
Redirect 301 old_file new_file
Next, replace old_file and new_file with the paths to your files. For example, if you have just renamed a file:
Redirect 301 /index.html /index2.html
Note in the example above that we have prefixed the filenames with a slash “/”. This slash is relevant to the location of the .htaccess file if you are setting this there, or the document root specified in the VirtualHost. A full path from the document root to the file needs to be provided. For example:
Redirect 301 /images/1345_t.jpg /images/thumbnails/1345.jpg
You can also redirect a file to another file at another domain by using it’s URL:
Redirect 301 /local_file.htm
Note that when redirecting the contents of a directory, you need to include the trailing slash to denote that it’s a directory that you are referencing.
When applying redirects the chances are that you are likely to have made multiple changes that may require them. Fortunately, you can include as many of these redirect rules as you require to successfully redirect traffic .