简体   繁体   English

在 Nuxt.js 中每条路由的末尾添加一个 slask /

[英]Add a slask / at the end of every routes in Nuxt.js

For a purpose of SEO i've been asked to add a slash at the end of every routes of my nuxt project.出于 SEO 的目的,我被要求在我的 nuxt 项目的每条路线的末尾添加一个斜线。 For example myapp.com/company should be myapp.com/company/ Is there a clean way to do that in Nuxt?例如 myapp.com/company 应该是 myapp.com/company/ 在 Nuxt 中有没有一种干净的方法可以做到这一点?

ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this : 好的,我通过在服务器端的中间件中编写重定向找到了解决方案,因此首先我将以下内容添加到nuxt.config.js中:

serverMiddleware: ["~/servermiddleware/seo.js"],

then i created this file /servermiddleware/seo.js : 然后我创建了这个文件/servermiddleware/seo.js:

const redirects = require('../301.json');
module.exports = function (req, res, next) {
  const redirect = redirects.find(r => r.from === req.url);
  if (redirect) {
    console.log(`redirect: ${redirect.from} => ${redirect.to}`);
    res.writeHead(301, { Location: redirect.to });
    res.end();
  } else {
    next();
  }
}

and finally i write the redirections i want in 301.json in the root folder: 最后我在根文件夹的301.json中写入我想要的重定向:

[
  { "from": "/company", "to": "/company/" }
]

edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution. 编辑:一个问题留在这里,因为应用程序链接的代码内部没有斜线,所以机器人的索引将使用它。...我需要找到一个更好的解决方案。

I am doing the requirement, too. 我也在做这个要求。 I do the task with the following method, which I do not know it is right. 我使用以下方法执行任务,但我不知道它是正确的。

Two steps: 两步:

Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. Nginx重写了URL,即在URL的末尾添加斜杠“ /”,该URL不以斜杠结尾。 In this case, the http request is sent to the web server. 在这种情况下,http请求将发送到Web服务器。

At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. 在另一种情况下,如果请求(或链接路由)在前端进行路由,则该请求不会将http请求发送到Web服务器。 Then add a middleware file called addSlash.js like this: 然后添加一个名为addSlash.js的中间件文件,如下所示:

function isThereSlashEnd(path) {
  let isSlash = true
  if (path) {
    let length = path.length
    isSlash = path[length-1] == '/' ? true : false
    console.log('??? path222: ', path, path[length-1], isSlash)
  }
  return isSlash
}
export default function({ req, store, route, redirect }) {

  /**
   * Add slash of '/' at the end of url
   */
  let isSlash = isThereSlashEnd(route.fullPath)
  console.log('??? path111: ', isSlash, route.fullPath, process.client)
  if (!isSlash) {
    if (process.client) {
      window.location.href = route.fullPath + '/'
      console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
    }
  }
}

With two steps above, get the task done. 通过上面的两个步骤,完成任务。

You can set trailingSlash to true in nuxt.config.js like below:您可以在 nuxt.config.js 中将trailingSlash设置为true ,如下所示:

router: {
    trailingSlash: true
}

For sitemap :对于站点地图

sitemap: {
    hostname: 'https://www.mywebsite.com',
    trailingSlash: true,
},

Details more on https://dev.to/mornir/nuxt.netlify-and-the-trailing-slash-3gge详情 请见 https://dev.to/mornir/nuxt.netlify-and-the-trailing-slash-3gge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM