Ad

Link Redirecting Assistance

Here's the script:

<script>
if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { 
document.location.href = 'https://thedomain.com/pages/bestsellers';
}
</script>

Question is, how can i make the code so that when i put https://thedomain.com/////////////////////////////////////////collections/all?sort_by=best-selling

How to incorporate var correctURL= document.location.href.replace(/([^:]/)/+/g, "$1"); whenever i duplicate any of the "/" "?" "_" "=" "-" characters in the web link.

Bottom line is i don't want to be forced to do this (its inefficient):

<script>
if(document.location.href.indexOf('https://thedomain.com/////////////////////////////////////////collections/all?sort_by=best-selling') > -1) { 
document.location.href = 'https://thedomain.com/pages/bestsellers';
}
</script>
Ad

Answer

I'm not 100% sure what you were asking , or why your url will ever have that many slashes in it , but you can just match just part of the url to see if the user is on the correct page.

if(document.location.href.indexOf('/collections/all?sort_by=best-selling') > -1) { 

     document.location.href = 'https://thedomain.com/pages/bestsellers';
}
Ad
source: stackoverflow.com
Ad