redirect-module icon indicating copy to clipboard operation
redirect-module copied to clipboard

Removing trailing slash with redirects

Open J4VMC opened this issue 5 years ago • 5 comments

I'm using your module to remove all trailing slashes from URLs to try and mitigate all the content duplication and to ease the pain that it'd be to do it for Netlify.

Within my nuxt.config.js file I've got the following:

  redirect: [
    {
      // eslint-disable-next-line no-useless-escape
      from: '^.*(?<=\\/)$',
      to: (from, req) => req.url.replace(/\/$/, '')
    }
  ],
  router: {
    trailingSlash: false
  },

I got that code from SO and modified the negative lookbehind to a positive lookbehind. The problem that I've got is that homepage doesn't load by itself, not even when accessed directly, only when clicked from somewhere else, like accessing the about page and then clicking on the logo. Any ideas why?

Also, is it possible to add statusCode: 301?

Thanks.

J4VMC avatar Feb 04 '20 00:02 J4VMC

I've found a solution which doesn't include the home page but includes the query parameters. The home page seems to be handled somehow, even though I'm using the trailingSlash: false setting from Nuxt Documentation

Inside your redirect list

{
    // eslint-disable-next-line
    from: '(?!^\/$|^\/[?].*$)(.*\/[?](.*)$|.*\/$)',
    to: (from, req) => {
      const base = req._parsedUrl.pathname.replace(/\/$/, '');  // We take pathname instead of req.url because of the query parameters
      const search = req._parsedUrl.search;
      return base + (search != null ? search : '');
    },
    statusCode: 301
  },

I've wrote a longer answer here if you want more details or have found a better solution: https://stackoverflow.com/a/60621932/4013333

damtsnkff avatar Mar 10 '20 16:03 damtsnkff

Hey @damtsnkff! This worked on the client-side for me but when I statically render it and the do npm run start, it does not work for me. Can you help me out?

Dhruvi16 avatar Feb 02 '21 09:02 Dhruvi16

Hey @Dhruvi16,

I just noticed someone gave another solution on the stackoverflow url I posted. (https://stackoverflow.com/a/60621932/4013333)

It's been quite some time I haven't worked on that issue, and I've never used the static rendering so not sure I can be helpful. But if you still can't find a solution, reach out with some additional code (or repro repository), and I'll try to help. :)

damtsnkff avatar Feb 02 '21 15:02 damtsnkff

Hey @damtsnkff! Yes! I tried every other way mentioned in that query. But I am unable to get the results. Also, I am hosting my static website on S3 if there is some way to set up redirect there, that would be awesome too. Thanks

Dhruvi16 avatar Feb 03 '21 00:02 Dhruvi16

@damtsnkff Idk if I'm missing something, but for me this only works when there are no more than 2 trailing slashes, in case when there are 2+ then I have 404.

Example: domain.com/ -> domain.com domain.com// -> domain.com domain.com/// -> 404 not found

evgenyny avatar Oct 26 '22 12:10 evgenyny