Javascript Redirect from 404 page

I appreciate any help here, I’m pretty much a newbie.

I’m trying to redirect links from oscommerce to a new site. You can add aliases to pages in the new CMS, but the field won’t accept ?, &, or = characters.

URL example would be /catalog/index.php?cPath=24_137_38 or /catalog/product_info.php?cPath=21_3&products_id=154

I need to replace those characters with a slash. /catalog/product_info.php?cPath=21_3&products_id=154 would become /catalog/product_info.php/cPath/21_3/products_id/154

So on the 404 page I need to fire a javascript file on page load, then construct a path to the alias, and then the CMS would route to the new page.

I know there are much better ways to do 302 redirects, but this is what I’m stuck with according to the developers, so I have to do what I can this way.

I’m reading about window.location and string replace but it’s not coming together for me. This is as far as I’ve gotten.

var currentURL = window.location.pathname;
newURL = currentURL.replace(/^[&=?]$/,'/')

I’ve tried different regular expressions, but nothing is working so far.

I have an update. Turns out pathname was wrong. I changed it to window.location.href and got the full URL.
I also put a pipe | in between the characters and got the right url back.

var currentURL = window.location.href;
newURL = currentURL.replace(/[?|=|&]/g, "/")

I don’t know if that’s all I need to do, I’m going to try and reference the js file on the 404 page and see what happens.

Nope didn’t work. How do I pass the new url? a reload?

Hi there,

Two things:

  1. This would be much easier to do with a .htaccess file. This is a text file that you can upload to the root directory of your web space. By specifying rewrite rules in this file, you can have the server rewrite the incorrect urls to correct ones and send the correct status code (301). This does however require your server to support this, as well as some degree of access to your server, which you might or might not have.
  2. Assuming you are stuck with using JS to do this, could you give an example of an incorrect url, as well as its correct counterpart.
Off Topic:

Gotta love Pink Floyd :slight_smile:

Yes an .htaccess file would be ideal, but not possible. The site is on Azure, and I can’t even get to IIS for some reason.

So I have the javascript working, and it goes to the new page, but if you go to a 404 and there isn’t a redirect it just sits there and loops. I need to stop it somehow.

Here is the latest:

var currentURL = window.location.href;
newURL = currentURL.replace(/[?|=|&]/g, "/");

I’ll PM you the URL.


Got your PM.
The problem is that (at least in the code you posted), you are not checking for a successful match before doing the redirect.

Presumably doing something like this should help:

var currentURL = window.location.href;
if (/[?|=|&]/.test(currentURL)){
  newURL = currentURL.replace(/[?|=|&]/g, "/");

You. Rock…even if you weren’t a Pink Floyd fan :slight_smile:

The test worked, I’ll keep testing and updating links in the CMS, I really appreciate it!