在 Nuxt.js 中的每条路线末尾添加一个 slask /
Add a slask / at the end of every routes in Nuxt.js
出于 SEO 的目的,我被要求在我的 nuxt 项目的每条路线的末尾添加一个斜杠。例如 myapp.com/company 应该是 myapp.com/company/ 在 Nuxt 中有没有一种干净的方法来做到这一点?
好的,我通过在服务器端的中间件中编写重定向找到了解决方案,所以首先我添加到 nuxt.config.js 中:
serverMiddleware: ["~/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();
}
}
最后我在根文件夹的 301.json 中写下了我想要的重定向:
[
{ "from": "/company", "to": "/company/" }
]
编辑:问题留在这里,因为应用程序链接的代码内部没有斜线,所以机器人的索引将使用它....我需要找到更好的解决方案。
我也在做这个要求。我用下面的方法做任务,不知道对不对。
两步:
Nginx改写了url,即在url末尾加了'/'的斜杠,url没有以斜杠结尾。在这种情况下,http 请求被发送到 web 服务器。
另一种情况,如果请求(或link路由)在前端被路由,则该请求不会向web服务器发送http请求。然后添加一个名为 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)
}
}
}
以上两步,完成任务。
出于 SEO 的目的,我被要求在我的 nuxt 项目的每条路线的末尾添加一个斜杠。例如 myapp.com/company 应该是 myapp.com/company/ 在 Nuxt 中有没有一种干净的方法来做到这一点?
好的,我通过在服务器端的中间件中编写重定向找到了解决方案,所以首先我添加到 nuxt.config.js 中:
serverMiddleware: ["~/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();
}
}
最后我在根文件夹的 301.json 中写下了我想要的重定向:
[
{ "from": "/company", "to": "/company/" }
]
编辑:问题留在这里,因为应用程序链接的代码内部没有斜线,所以机器人的索引将使用它....我需要找到更好的解决方案。
我也在做这个要求。我用下面的方法做任务,不知道对不对。
两步:
Nginx改写了url,即在url末尾加了'/'的斜杠,url没有以斜杠结尾。在这种情况下,http 请求被发送到 web 服务器。
另一种情况,如果请求(或link路由)在前端被路由,则该请求不会向web服务器发送http请求。然后添加一个名为 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)
}
}
}
以上两步,完成任务。