I have a set of links with #anchors pointing to a single webpage and I would like to smoothly move to a model with a separate webpage for each of those links. I want the old links to keep working using a redirect.
Old link style:
/all_products#A
/all_products#B
/all_products#C
New link style:
/products/A
/products/B
/products/C
I know that the server does not receive the #anchor name in the request but Javascript might.
Is it possible to automatically redirect from /all_products#A to /products/A using Javascript?
JQuery would be fine, it's being used on the site anyway.
this might help:
I hope this can help :)
Put this as close to the top of your HTML
<head>
as you can so that it can execute before the rest of the page resources download:This will check the current page URL and redirect if it matches the old-style path.
This code makes use of the
window.location
object which contains all the parts of the current URL already split up into component parts.Making this script more generic is left as an exercise for the implementer.
With jquery, either just replace the href with the correct one:
or capture clicks and redirect:
I added this new answer to include some best practices for both extracting the hash from the url and doing a redirect.