tags:

views:

50

answers:

3

Hi people.

I'm having a bit of trouble figuring out how to do this one.

Here's the scenario. My e-commerce site has a blog with a lot of content, promoting the products I sell, reviewing them etc. 99% of the posts on the blog link back to the products they are talking about.

I'm going to be changing the URL where my ecommerce system is located. Therefore my blog is going to end up sending people to a lot of 404 pages.

The idea I've had is to be able inclue a JS file which will on load, scan the document for existing links then, if found, swap them out with ones which are now correct.

I hope this make sense.

Cheers, Jim


+5  A: 

This is pretty simple to do in jQuery; but a bad idea on multiple levels. The first being as ItzWarty said, users with JavaScript disabled will still get 404's.

Another being that web spiders such as GoogleBot don't execute JavaScript, so they'll see 404's too- ruining your SEO; when migrating content to a new URL you should 301 redirect (moved permanently) the old URLs to the new location if you have any interest in retaining your search rankings.

Steve
Redirecting old links to the new URLs on the server side is the better way to go here. If everything gets done on your end (the server-side) you can be sure that the redirect happened properly, without having to worry about whether the Javascript was run on the user end or not.
bta
I like that you brought up web spiders and SEO. I hadn't thought of that at all, and with a for-profit site, that is a critical factor.
Al Crowley
+3  A: 

If you are going to create the JavaScript to redirect, why not just create a batch process to update all those files/links.

I am assuming these are static text files, if it is database driven it is also achievable.

Dustin Laine
A: 

In your blog pages stick this simple piece of code at the end of the pages just before the tag.

<script type="text/javascript">
   var collection = document.getElementsByTagName('a'),
   old_url = "http://www.myoldurl.com", //place here you old url
   new_url = "http://www.mynewurl.com", //place here your new url
   i, len = collection.length;
   for(i = 0; i < len; ++i)
      if(collection[i].href)
         collection[i].href = collection[i].href.replace(old_url, new_url);
</script>

Since you are using a blog, you simply need to stick this piece of code once for all in the layout/template page of the blog thus the code will be inserted on each page just befor ethe tag.

Marco Demajo