问几个关于SEO的Nodejs相关问题,不懂怎么搜

问几个关于SEO的Nodejs相关问题,不懂怎么搜

untitled1.png

untitled2.png

想问大家几个问题:

  1. rubychina 在 google 中为何能显示那么丰富的版块信息?
  2. 我们社区的描述部分看起来好乱,我应该改进哪个地方?
  3. 为何 ruby-china 有搜索框?
  4. 论坛现在的帖子有 4w 多了,如果我想提供 sitemap 给搜索引擎的话,搜索引擎一般都不允许单个的 sitemap 超过 5w 行。超过 5w 行时推荐拆成子 sitemap。 请问我该如何拆分好呢?我以每用户来拆分 sitemap 的话,是不是太细了?

5 回复

好的,以下是根据你的需求重新编写的帖子内容:


问几个关于SEO的Nodejs相关问题,不懂怎么搜

问题背景

我在开发一个社区网站,并且希望优化其SEO效果。以下是几个具体问题,希望能得到一些指导。

问题列表

  1. RubyChina 在 Google 中为何能显示那么丰富的版块信息?
  2. 我们社区的描述部分看起来好乱,我应该改进哪个地方?
  3. 为何 Ruby-China 有搜索框?
  4. 论坛现在的帖子有 4w 多了,如果我想提供 sitemap 给搜索引擎的话,搜索引擎一般都不允许单个的 sitemap 超过 5w 行。超过 5w 行时推荐拆成子 sitemap。请问我该如何拆分好呢?我以每用户来拆分 sitemap 的话,是不是太细了?

解答与示例代码

1. 如何让站点在Google中显示丰富的版块信息?

为了使站点在Google中显示丰富的版块信息,可以使用结构化数据(Schema.org)。例如,使用@typeWebSiteBreadcrumbList来标记站点的结构。

const schemaOrg = {
    "@context": "https://schema.org",
    "@type": "WebSite",
    "name": "My Community",
    "url": "https://example.com",
    "potentialAction": {
        "@type": "SearchAction",
        "target": "https://example.com/search?q={search_term_string}",
        "query-input": "required name=search_term_string"
    },
    "breadcrumb": {
        "@type": "BreadcrumbList",
        "itemListElement": [{
            "@type": "ListItem",
            "position": 1,
            "name": "Home",
            "item": "https://example.com"
        }, {
            "@type": "ListItem",
            "position": 2,
            "name": "Forums",
            "item": "https://example.com/forums"
        }]
    }
};

2. 改进社区描述部分

确保你的页面元数据清晰且描述性强。可以使用<meta>标签来设置描述和关键词。

<meta name="description" content="欢迎来到我们的社区,这里汇聚了各种技术爱好者,分享知识,交流经验。">
<meta name="keywords" content="技术, 社区, 讨论, 分享">

3. 为何 Ruby-China 有搜索框?

搜索框可以提高用户体验和SEO。用户可以通过搜索更快地找到他们需要的信息。使用Node.js框架如Express,可以轻松实现搜索功能。

const express = require('express');
const app = express();

app.get('/search', (req, res) => {
    const query = req.query.q;
    // 进行数据库查询并返回结果
    res.send(`搜索结果: ${query}`);
});

app.listen(3000, () => console.log('Server running on port 3000'));

4. 如何拆分 sitemap?

你可以基于时间或类别来拆分sitemap。例如,按月生成sitemap。

const fs = require('fs');

function generateSitemap(month) {
    const posts = getPostsByMonth(month); // 假设有一个函数获取特定月份的帖子
    const sitemapContent = `<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        ${posts.map(post => `<url><loc>https://example.com/post/${post.id}</loc></url>`).join('')}
    </urlset>`;
    
    fs.writeFileSync(`./sitemaps/sitemap-${month}.xml`, sitemapContent);
}

generateSitemap('2023-09');

希望这些解答对你有所帮助!


sitemap.xml 文件可以指定板块, 就是不知道怎么调试那东西

Google搜索引擎的爬虫遵循Making AJAX Applications Crawlable规约。

几个要点是:

  • 在首页页面源代码中加入<meta name="fragment" content="!">
  • URL中所有以#开始的片段,都修改为#!,服务端路由处理函数做相应的处理;
  • 在sitemap中列出希望爬虫抓取的URL

Fecth as Google工具测试效果。

Bing和Yahoo也支持Google爬虫规范。

可以借鉴prerender node的实现思路:如果HTTP请求头部中包含_escaped_fragment_,或者UserAgent字符串中有搜索引擎爬虫的标志,则返回一个自定义的页面快照。

是这个吗?

对于用户搜索一些基本的常识问题,Google会在排名第一的位置直接给出答案,如搜索where is the forbidden city, where is terracotta army等,不显示答案的来源链接,这会大大减少用户点击链接进入来源网站的机会,从而减少网站的访问量。

When Google Shows A Source Or Credit For Quick Answers & Knowledge Graph Google says it doesn’t provide a source link when the information is basic factual data. Barry Schwartz on September 15, 2014 at 9:03 am • 535 • • • • More

As Google continues to provide quick answers and knowledge graph data directly at the top of the search results, publisher concerns continue to rise. Publishers are worried that searchers will have less of a need and desire to click through from Google’s search results page to the publisher’s page, leaving the publishers with very little methods to monetize their content, thus reducing their ability to add more useful content to their site over time. I asked Google recently, when and how do they decide to provide a “source” link in the search results when they show quick answers or knowledge graph data. For example, a search for [what is SEO] returns a quick answer from Wikipedia with a link to Wikipedia:

But a search for [how old is obama] just gives the answer, without citing a source:

A Google spokesperson tells us that when the information is “basic factual information you can find many places (e.g., when Obama was born),” they will “just present it as is” without providing a source. But when the information is “not widely-known information” or when they “show relevant snippets from webpages,” Google will “typically show the source.” Google added that in “some cases”, such as when they are “working directly with the source” they may not show the source. This last example of not showing the source when working directly with the source is with the iPhone 6 answers that came up in Google last week.

针对您提到的问题,我将逐一进行解答,并提供一些示例代码。

1. 如何让网站在Google中显示丰富的版块信息?

要实现这一效果,通常需要使用Schema.org标记来增强您的网页内容。这可以帮助搜索引擎理解页面结构并展示更丰富的内容。

示例代码:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "name": "首页",
    "item": "https://www.example.com/"
  },{
    "@type": "ListItem",
    "position": 2,
    "name": "讨论区",
    "item": "https://www.example.com/discussion"
  }]
}
</script>

2. 如何优化描述部分以提高可读性?

确保每个页面都有一个清晰、简明且独特的<meta name="description">标签,这样有助于搜索引擎更好地理解页面内容。

示例代码:

<meta name="description" content="这里是Ruby China的讨论区,你可以在这里找到关于Ruby的各种信息和资源。">

3. 为什么Ruby-China有搜索框?

搜索框可以提高用户体验,让用户能够快速查找相关内容。这不仅对用户友好,也能增加用户的停留时间。

4. 如何生成和拆分sitemap?

为了满足搜索引擎的要求,您可以将大文件拆分成多个小文件。通常的做法是根据日期或分类来拆分。

示例代码:

const fs = require('fs');
const url = require('url');

// 假设你已经有了所有URL数据
let urls = [...];

// 每个文件最多包含5万条URL
const MAX_URLS_PER_FILE = 50000;

urls.forEach((urlData, index) => {
  let fileName = `sitemap-${Math.floor(index / MAX_URLS_PER_FILE)}.xml`;
  
  // 构造XML格式
  let xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>${urlData}</loc>
  </url>
</urlset>`;

  fs.appendFile(fileName, xmlContent, err => {
    if (err) throw err;
  });
});

这段代码会将URL列表分割成多个文件,每个文件包含最多5万个URL。

回到顶部