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!

3 Answers 3

2
+50
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 :)

Sign up to request clarification or add additional context in comments.

10 Comments

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
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?
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 stackoverflow.com/questions/4200157/… )
Since the regex is a string you need to double the backslashes.
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.
|
2

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

redirect: [
    {
        from: '^(\\/[^\\?]*[^\\/])(\\?.*)?$',
        to: '$1/$2',
    },
],

Comments

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 + '/';
      }
    }
  ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.