Blog Be Nimble

I've got cruftless links. Now what?

This is the (long awaited) follow-up article to my post about how cruftless links are better for SEO. If you haven't read that post be sure to do so first, as this article will make more sense at that point.

So now that we've established how awesome cruftless links are, many of you have already switched over to them; and the next question I always get is how to make sure the old links are redirected to the new. This is something we need to address for a number of reasons:

  • Chances are that search engines have your old links saved to their index, and this allows us to make sure they see and index the new ones. It's possible your ranking on SERPs (search engine results page) could drop if this isn't done.
  • It makes the transition painless for your website visitors. Nobody wants to visit your site and see a 404 Not Found error.

For most people the best way to tackle this problem is by using a .htaccess file and specifying something called "rewrite rules". If your web hosting uses Windows and IIS, then you can do (essentially) the same thing with what's called a web.config file, but I won't be covering that in this article.

Before we get started you'll need to make sure that your hosting provider meets the requirements for what we're about to do. I have a list of compatible hosting companies that we know support this, but the list isn't exhaustive. Even if your hosting company isn't listed that doesn't mean this won't work, only that you'll need to confirm the following:

  • Your hosting uses the Apache web server (most companies do)
  • The mod_rewrite module for Apache is installed and enabled (again, most companies already do this)

Depending on the configuration your hosting company uses, you may need to enable the .htaccess file in your hosting control center. Also, be sure that you have FTP access to your hosting account, as you'll need to edit/upload the .htaccess file directly to your hosting account, in the same folder where your homepage is located. If you already have a .htaccess file in that location, don't delete or replace it. Instead, open it in a good text editor, and add your code to the end of that file. With all that said, let's get started.

Let's say your old link looked like this:

And you've updated that to a shiny, new cruftless link, like so:

Obviously the goal is to make sure that anyone who tries to visit the old link is redirected to the new link. We can do this in the .htaccess file using this bit of code:

RewriteEngine On
Redirect 301 /photos.html

Looks fairly simple, and I'm guessing you can read this and make out what is going on. Let's walk through the code line by line.

The RewriteEngine On line does exactly what you might expect. It turns on the engine you'll be using to redirect visitors to the page you want them to see. We need to make sure this is turned on first, before starting our redirect code.

The next line is where the magic happens. The Redirect command is quite simple - you're telling the server where your website is located that you want to redirect something. But we also need to provide some additional information - more specifically, what type of redirect this is, the old page that people will be trying to access, and the new page where you want them to be redirected.

The 301 number is specific type of redirect, called a "permanent" redirect. This tells web browsers and the search engines crawling your site the old page has been permanently replaced with the new one. By using this "permanent" redirect, search engines will automatically update their indexes to use the new cruftless link.

After that we have the /photos.html portion. This is the old page that people (and search engines) will be trying to access.

Finally we have the portion. You'll recognize this as our new cruftless link, and it's where people who try to access the old page will be redirected.

If you have more than one page to redirect, you can add as many Redirect lines as you need. Here is an extended example, based on the same code above:

RewriteEngine On
Redirect 301 /photos.html
Redirect 301 /reported_sitings.html
Redirect 301 /contact/contact.php

Note that we only need to use the RewriteEngine On line once. The last line also shows how to target sub-pages in your site. As a reminder, the .htaccess file should be placed in the same location as your homepage. If there's already a .htaccess in that location, just open the file and add your code to the end.

Of course, be sure to test our your redirects when your done, to make sure they are working correctly.

EDIT: As requested in the comments, here is how to redirect any 404 Not Found errors to the homepage. This will work anytime a visitor tries to access a page or file that can't be found on your website, and can be done in the .htaccess file like so:

ErrorDocument 404

This is a good fallback method if you don't remember all the old URLs and want to make sure visitors can still navigate your site even if they try to access a page that doesn't exists anymore.

Before wrapping up this article I want to make sure we cover the importance of a good text editor. You don't want to be using a program like TextEdit or Notepad, as they oftentimes will add extra data to the file, or impose a certain file extension (like, .txt, .rtf, etc.). Instead, be sure you are using a text editor designed to work with code. This is easy to do, and free! For Mac users there is the free TextWrangler from Bare Bones Software. For Windows users I'd suggest the free Notepad++ (not to be confused with the regular Notepad app included with Windows by default).

Another note specifically for Mac users. By default Mac OS X hides files that start with a period, such as .htaccess files. As such I recommend that you create the file on your hosting account directly. You can then right-click on the .htaccess file in your FTP program, and from the contextual menu select to edit the file in your text editor of choice. The nice thing about this approach is that your FTP client should automatically upload the file when you save your changes. Easy!

Hope this helps you make the transition to cruftless links. Make sure to leave your feedback in the comments below.