11

This question is based in this previous one

I want all my URL's to end with slash for SEO reasons. So far I have this function working with nuxt-redirect-module

redirect: [
    {
        from: '^.*(?<!\/)$',
        to: (from, req) => req.url + '/'
    }
]

This checks the url, and adds a / at the end in case that there is not. The problem is when the url has params at the end.

So right now, this redirects

https://example.com/folder to

https://example.com/folder/ (intended behaviour)

But with params, right now it works like this:

https://example.com/folder?param=true to

https://example.com/folder?param=true/ (it adds the / after the params)

QUESTION

Which would be the way to make it so it would redirect instead from

https://example.com/folder?param=true to

https://example.com/folder/?param=true (so it would add the / at the end of the url but before the params)

Thanks in advance!

Joe82
  • 1,357
  • 2
  • 24
  • 40

3 Answers3

2
redirect: [
    {
        from: '^[\\w\\.\\/]*(?<!\\/)(\\?.*\\=.*)*$',
        to: (from, req) => {
            const matches = req.url.match(/^.*(\?.*)$/)
            if (matches.length > 1) {
                return matches[0].replace(matches[1], '') + '/' + matches[1]
            }
            return matches[0]
        }
    }
]

Check the first regex here: https://regex101.com/r/slHR3L/1

Thanks to @Seybsen for the final hint :)

niccord
  • 764
  • 4
  • 20
  • Thanks for the answer. It seems that there is something wrong with that regexp in JS. It outputs this error `SyntaxError: Invalid regular expression: /^[w./]*(?<!/)(?.*=.*)*$/: Invalid group ` – Joe82 May 21 '19 at 08:49
  • 1
    backslashes are important in order to escape characters e.g. \w has a different meaning from w, the dot character means "everything" but \. it's used to specify the dot character itself. Are you sure you kept the right format? – niccord May 21 '19 at 09:03
  • Yep, I copied the expression as it is. I searched a bit and it seems there is some differences when it comes to regexp applied to JS and that could be the source of the error (something like this example https://stackoverflow.com/questions/4200157/javascript-regular-expression-exception-invalid-group ) – Joe82 May 21 '19 at 09:26
  • 1
    Since the regex is a string you need to double the backslashes. – Seybsen May 21 '19 at 12:07
  • Thanks for the input @Seybsen. I checked niccord edit but now it doesn't seem to work in any case (not even in the simple https://example.com/folder to https://example.com/folder/ case. It doesn't give an error thought. – Joe82 May 21 '19 at 13:00
  • can you please try adding a `console.log('something')` before `const matches`? – niccord May 21 '19 at 13:02
  • Did that—literally console.log('something'), and it outputs 'something' in my nuxt.js console, but just when I first edit my redirects file (the one where I have all the redirects, including this one), and then refresh the browser. If I just edit the file, or just refresh the browser, it doesn't output nothing. – Joe82 May 21 '19 at 13:44
  • Note: that regexp uses negative lookbehind feature which is supported from Node 9. – rchl Aug 27 '19 at 11:50
  • 1
    Note from the docs: `The redirect module will not work in combination with nuxt generate. Redirects are realized through a server middleware, which can only react when there is a server running. ` – Sascha Feb 28 '22 at 11:21
  • @Sascha - How can we add trailing slash to URL's with nuxt generate? Is there any work around for it? – Shreeraj May 27 '22 at 09:57
2

The following might be simpler and does the same as far as I can see:

redirect: [
    {
        from: '^(\\/[^\\?]*[^\\/])(\\?.*)?$',
        to: '$1/$2',
    },
],
maikel
  • 1,135
  • 1
  • 12
  • 9
0

I wrote it like this. This code catch all unsigned urls ? and / at the end of the line. I think that's enough

 redirect: [
    {
      from: '^(?!.*\\?).*(?<!\\/)$',
      to: (from, req) => {
        return req.url + '/';
      }
    }
  ],