Web Development for 2012

Alexander Dickson - Web Developer covering PHP, jQuery / Javascript, XHTML, CSS, more

Redirecting from an old URL to a new one

Filed under Apache. Published on Sunday, 13th September 2009.

Sometimes, it is necessary to moves the files or somehow change the URL of a page. To help SEO, help prevent link rot and in general be of assistance to your site's visitors, it is important to not let the old URLs become dead links. This is easy using a few lines in your .htaccess file. If you have access to Apache's httpd.con, you can place the redirects there.

I recently changed the URL that my jQuery plugins were located at. To ensure all of the old links still worked, I first went to Google and searched site:alexanderdickson.com. This gave me a small snapshot of what Google has indexed for my domain. Unfortunately, this is only a snapshot, so to see a more thorough listing, you will need to use Google's webmaster tools.

Once you have a list of all the old URLs, you will need to map them to the new ones. In some cases, a clever regex may work, however this is not always the case. Open up your .htaccess file, and add the line in example below.

RewriteBase / Redirect 301 /path/to/old/page /path/to/new/page
Redirection Example

This line makes the server send out a 301 redirect, that tells Google (and other clients) "This page has been permanently moved to this new location."

You can simply add a line for every old URL, and you'll be good to go.

Comments

  • Alexander Dickson

    Posted on Monday, 14th September 2009 @ 12:53pm.

    Thanks Nick! That's some useful info - I think I may update this article to make it bit more in-depth.

  • nickf

    Posted on Monday, 14th September 2009 @ 12:25pm.

    You should take a look at the Google Webmaster tools (https://www.google.com/webmasters/tools) for things like this. It's got some really handy reports, showing all the links to your site (internal or external) which Google knows about and if there are any problems with them. There's other stuff too, like submitting and checking your sitemap.xml file etc.

    There's also a really good question on Stack Overflow about using the Apache mod_rewrite if you want more complicated redirection. http://stackoverflow.com/questions/286004/hidden-features-of-modrewrite

Leave a Comment

Comment Details

Your email will never be displayed. If you have a gravatar, it will be displayed.

Note: Your comment may require approval before it is posted to the site.

Stack Overflow Profile

view full profile »

About

I'm a web developer from the Sunshine Coast, Australia. more »