Nuxt.js - 在所有 url 的末尾强制尾随斜杠 [英] Nuxt.js - force trailing slash at the end of all urls
本文介绍了Nuxt.js - 在所有 url 的末尾强制尾随斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来确保我的所有 url 都以斜杠结尾(所以首先检查末尾是否已经有斜杠,如果没有,则添加一个).
I'm looking for a way to make sure that all of my urls end with a trailing slash (so first check if there is already a trailing slash at the end, and if not add one).
我已经尝试过 nuxt-redirect-module,它可以添加斜杠但随后会导致无限重定向
I have tried with nuxt-redirect-module, and it works adding the slash but then it leads to an infinite redirect
redirect: [
{
from: '^(.*)$',
to: (from, req) => {
let trailingUrl = req.url.endsWith('/') ? req.url : req.url + '/'
return trailingUrl
}
}
]
欢迎提供任何见解.谢谢!
Any insight will be welcome. Thanks!
推荐答案
您可以尝试只匹配那些不以斜线结尾的 URL:
You can try to match only those URLs that do not end with a slash:
redirect: [
{
from: '^.*(?<!\/)$',
to: (from, req) => req.url + '/'
}
]
这篇关于Nuxt.js - 在所有 url 的末尾强制尾随斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文