avatar
07.【个人网站】提高网站SEO效果策略配置
✔️

07.【个人网站】提高网站SEO效果策略配置

published_date
最新编辑 2024年09月08日
slug
提高网站SEO效果策略配置
tags
Website

配置站点地图

在 React 的 Next.js 项目中添加站点地图可以通过以下几个步骤实现

步骤1:安装依赖包

使用 next-sitemap 这个包来自动生成站点地图
npm install next-sitemap // or yarn install next-sitemap

步骤2:配置 next-sitemap

在项目根目录下创建 next-sitemap.config.js 文件,并进行配置:
js复制代码 module.exports = { siteUrl: process.env.SITE_URL || 'https://www.yoursite.com', // 替换为你的站点URL generateRobotsTxt: true, // 生成 robots.txt 文件 // 其他选项可以在这里配置 };

步骤 3: 修改 next.config.js 文件

确保在你的 next.config.js 文件中包含 next-sitemap 的配置:
js复制代码 // next.config.js module.exports = { // 其他Next.js配置 env: { SITE_URL: 'https://www.yoursite.com', // 替换为你的站点URL }, };

步骤 4: 生成站点地图

package.json 文件的 scripts 部分添加生成站点地图的脚本:
json复制代码 "scripts": { "postbuild": "next-sitemap" }
每次运行 npm run build 时,将自动生成站点地图文件

步骤 5: 部署和验证

  1. 构建项目:运行 npm run build
  1. 部署项目:将构建后的项目部署到你的服务器或托管服务。
  1. 验证站点地图:访问 https://www.yoursite.com/sitemap.xmlhttps://www.yoursite.com/robots.txt 以确保站点地图和 robots.txt 文件正确生成。

示例配置

以下是一个完整的示例配置:
next-sitemap.config.js
module.exports = { siteUrl: process.env.SITE_URL || 'https://www.yoursite.com', generateRobotsTxt: true, // 你可以在这里添加更多选项,例如忽略某些路径 exclude: ['/admin/*'], robotsTxtOptions: { policies: [ { userAgent: '*', allow: '/' }, { userAgent: '*', disallow: '/admin' }, ], }, }
next.config.js
module.exports = { env: { SITE_URL: 'https://www.yoursite.com', }, };
package.json
{ "scripts": { "postbuild": "next-sitemap" } }

搜索引擎收录

在Google主动绑定站点

  1. 注册登录 https://search.google.com/search-console
  1. 验证域名
  1. 提交站点地图
notion image
notion image

About

My desire to practice my skills and share my acquired knowledge fuels my endeavors.

Contact Me : znjessie858@gmail.com

Subscribe

Subscribe to receive notifications of my latest posts and unsubscribe at any time!