Nuxt.js - force trailing slash at the end of all urls

安稳与你 提交于 2019-12-02 14:06:51

问题


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).

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!


回答1:


You can try to match only those URLs that do not end with a slash:

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


来源:https://stackoverflow.com/questions/54346345/nuxt-js-force-trailing-slash-at-the-end-of-all-urls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!