首页 > 解决方案 > 阻止特定域路径中的 Gatsby 页面出现在 Google 搜索中

问题描述

是否可以阻止特定域路径中的 Gatsby 页面出现在 Google 搜索中。

我不想阻止整个网站出现在 Google 搜索中,只是在特定路径中存在一个页面。例如www.mywebsite.co.uk/hide-me

标签: javascriptreactjsseogatsby

解决方案


使用该<Helmet>组件设置机器人的meta标签。

const HideMe = ()=>{
  return <>
    <Helmet>
      <meta name={`robots`} content={`noindex, nofollow`} />
    </Helmet>;
    <h1>I'm the Hide Me page </h1>
  </>;

大多数 Gatsby 启动器都带有一个SEO组件,其中包含 robotsmeta标记等,如果是这种情况,您可能需要为每个传递正确prop. 例如:

const SEO = ({ index, follow, description, lang, meta, title }) => {
  const { site } = useStaticQuery(graphql`
              query getAllMetadata {
                  site {
                      siteMetadata {
                          title
                          description
                          author
                      }
                  }
              }
    `,
  );
  const metaDescription = description ?? site.siteMetadata.description;

  return <Helmet htmlAttributes={{ lang }} title={title} titleTemplate={`%s | ${site.siteMetadata.title}`}>
    <meta name={`robots`} content={`${index ? `index`:`noindex`},${follow ? `follow`:`nofollow`}`}/>
    <meta name={`description`} content={metaDescription}/>
    <meta name={`og:title`} content={title}/>
    <meta name={`og:description`} content={metaDescription}/>
    <meta name={`og:type`} content={`website`}/>
    <meta name={`twitter:card`} content={`summary`}/>
    <meta name={`twitter:creator`} content={site.siteMetadata.author}/>
    <meta name={`twitter:title`} content={title}/>
    <meta name={`twitter:description`} content={metaDescription}/>
  </Helmet>;
};

注意:我假设两者都是index布尔follow值,但可以根据需要自定义它或者更适合你的任何东西

如果这是您的情况,在您的/hide-me页面中,您将有类似的内容:

const HideMe = ()=>{
  return <>
    <SEO index={false} follow={false} />
    <h1>I'm the Hide Me page </h1>
  </>;

rel此外,您需要将属性添加到指向该页面的所有链接,nofollow以避免被 Google 的爬虫跟踪。

<Link rel={`nofollow`} to={`/hide-me`}>Untracked link</Link>

此外,您可能还想从(如果有的话)文件中删除该页面,您可以使用带有规则sitemap.xml的站点地图插件之一轻松实现它。gatsby-plugin-sitemapexclude


推荐阅读