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.
UPDATE:
Nope didn’t work. How do I pass the new url? a reload?
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, "/");
window.location=newURL;
I’ll PM you the URL.
Hi,
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, "/");
window.location=newURL;
}
You. Rock…even if you weren’t a Pink Floyd fan 
The test worked, I’ll keep testing and updating links in the CMS, I really appreciate it!
Thanks!