Nuxt.js-使用参数在URL的末尾添加斜杠

Joe*_*e82 9 vue.js vuejs2 nuxt.js

这个问题是基于上一个问题

由于SEO的原因,我希望所有URL都以斜杠结尾。到目前为止,我已经将此功能与nuxt-redirect-module一起使用

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

这将检查url,并/在末尾添加一个,以防万一。问题是当url末尾有参数时。

所以现在,这个重定向

https://example.com/folder

https://example.com/folder/ (预期行为)

但是使用params,现在它的工作方式如下:

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

https://example.com/folder?param=true/(它/ 在params之后添加)

这将是实现它的方法,因此它将从

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

https://example.com/folder/?param=true (因此它将/在url的末尾但在params之前添加)

提前致谢!

nic*_*ord 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]
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

在此处检查第一个正则表达式:https://regex101.com/r/slHR3L/1

感谢@Seybsen 的最后提示:)