NodeJS routing end in / or not

Is there a way to ensure that my NodeJS website always redirects to either with trailing / or without

So
https://www.example.com/my-page
AND
https://www.example.com/my-page/

Are not 2 different URLs but rather i can choose to always redirect a user to always an ending / option?

Just an update on this, as I still have not managed to work it out

@sid I saw you have a similar thing on your site, where it works with or without, do you possibly have any idea how to try make it one or the other?

Which one? I can share screenshot of the routes panel.

Like this one, where the page shows as
https://www.pukkatravels.com/product/t/en-us/tours/dogsledding-adventure-in-tromso


OR as
https://www.pukkatravels.com/product/t/en-us/tours/dogsledding-adventure-in-tromso/

There must be a way to choose a standard, and always have one version redirect to the other, whatever your preference was.

Shared privately.

1 Like

Thanks Sid, as I kind of expected you have done your routing very similar to mine with some minor differences, but also have an end result of prefixed forward slash or not both going to the same page which is a potential SEO issue if some backlink partners decide to backlink with or without, and the SERPS see it as 2 unique URLs.
Personally I am not sure if the entire issue is actually resolved just by making sure a canonical tag is used for every page that explicitly tells the SERP which URL you want to use, as that can not change either way.

Even in that article i linked to, they noted that javascript frameworks with routing work a bit different from the other ways like wordpress / htaccess

@ben, I know you take SEO as seriously as me, can you give me your thoughts when you have a gap.

hello guys, did you manage to force slash at the end of the URL?

I never actually got around to it to be honest, its still without a trailing slash.

1 Like

thanks @psweb!! Will try here too!

I don’t know the SEO implications, but I’ve had success parsing the URL when it doesn’t match a route and rerouting via a 301

For large sites, a DB lookup often happens for a server-side rendered 301 page (as anything else might be seen as a malicious redirect by modern browsers)

The way I think of it is instead of giving the user/crawler a 404-type response when they go to the non-setup route, they’re 301’d from site.org/route to the correct route as I define it site.org/route/

This may be poor practice for SEO; in our cases we just wanted it to work for users typing in the URLs

@nomad exactly what I did!

 exports.handler = function (app) {
   app.get("/", (req, res) => {
   res.redirect(301, "/login" + "/");
  });

  app.use((req, res, next) => {
    if (req.path.indexOf("/api/") === -1) {
      if (req.path.substr(-1) === "/") {
        req.url = req.url;
      } else {
        if (
          req.path.indexOf("/login") > -1 ||
          req.path.indexOf("/create_account") > -1
        ) {
          if (!req.query._ga) {
            res.redirect(301, req.url + "/");
          }
        }
      }
    }
    next();
  });
};

Some of the pages have google analytics parameter…so I needed to create a condition for it.
Any suggestion is always welcome!

1 Like

Hi! I need a solution on that, I’m facing the same problem but cause I’m a newbie I don’t know how to achieve that in my node wappler app.

I saw this: https://www.npmjs.com/package/connect-slashes

Can I install this and that’s it?

I would appreciate so much! I don’t even know wich files I have to modify or how to install it :disappointed_relieved:

Thank you guys.

Came to this solution after reading your comments, don’t know if is 100% correct but seems to work:

Inside extensions/server_connect/routes I placed a file called nonslash.js:

exports.handler = function (app) {
app.use((req, res, next) => {
    // Check if the path ends with a slash and is longer than 1 character
    if (req.path.endsWith('/') && req.path.length > 1) {
        const query = req.url.slice(req.path.length);
        const newPath = req.path.slice(0, -1) + query;
        return res.redirect(301, newPath);
    }
    next();
});

};

Restarted services, it’s working!