Internal url redirect

Solved
deibar
New Member
1 0 0

Hello everyone,

How to redirect all internal url links in one time? For example if i have url:

https://abc.com/collections/all/

and I want redirect to:

https://abc.com/collections/all 

that it would be without / 

Thanks!

0 Likes
samthewebb
Shopify Partner
24 3 2

This is an accepted solution.

Have you tried using Shopify's redirects? https://help.shopify.com/en/manual/online-store/menus-and-links/url-redirect

Honestly, I've never tried it but my gut feeling is that since /collections/all and /collections/all/ are still in a way the same route, it might just redirect back to itself over and over again on repeat forever. Maybe not though, and it might be worth giving a shot.

There's also a front end solution, but I wouldn't necessarily recommend that you do it. But it works so you can use it if you want. You could use window.location.pathname to do it. Here's a function that would redirect if the pathname ends with a / but only if it's not the homepage because the pathname is always / for the homepage. Just add it as high as possible on the site to prevent the whole page from having to load twice.

<script>
(function(){
  var pathname = window.location.pathname;
  if (pathname && pathname.length > 1 && pathname.charAt(pathname.length - 1) === '/') {
    window.location.pathname = window.location.pathname.slice(0, -1);
  }
})();
</script>
Sam Webb
https://samthewebb.com
sam@samthewebb.com