【问题标题】:X-Robots noindex specific page in .htaccessX-Robots noindex .htaccess 中的特定页面
【发布时间】:2012-11-01 14:49:24
【问题描述】:

我可以在 .htaccess 中使用 x 机器人“noindex,follow”特定页面吗?

我找到了一些关于 noindexing 类型文件的说明,但是我找不到 noindex 单个页面的说明,并且到目前为止我尝试过的方法没有奏效。

这是我要查找的页面 noindex:

http://www.examplesite.com.au/index.php?route=news/headlines

这是我迄今为止尝试过的:

<FilesMatch "/index.php?route=news/headlines$">
 Header set X-Robots-Tag "noindex, follow"
</FilesMatch>

感谢您的宝贵时间。

【问题讨论】:

  • FilesMatch 参数是一个正则表达式,因此您可能必须“转义”一些特殊字符,例如 .?。我现在无法测试,但我想测试"/index\.php\?route=news/headlines$"
  • 在 PHP 中处理这种情况要比在 .htaccess 中容易得多;你不能直接调用header('X-Robots-Tag: noindex, follow'),可能在news 控制器中的headlines 操作中(如果这确实是一个MVC 架构)?

标签: .htaccess http-headers nofollow noindex x-robots-tag


【解决方案1】:

似乎不可能从 .htaccess 文件中匹配请求参数。以下是您可以匹配的列表:http://httpd.apache.org/docs/2.2/sections.html

在您的脚本中执行此操作会容易得多。如果您在 PHP 上运行,请尝试:

header('X-Robots-Tag: noindex, follow');

您可以轻松地在 $_GET、REQUEST_URI 等上构建条件。

【讨论】:

    【解决方案2】:
    RewriteEngine on
    RewriteBase /
    
    #set env variable if url matches
    RewriteCond %{QUERY_STRING} ^route=news/headlines$
    RewriteRule ^index\.php$ - [env=NOINDEXFOLLOW:true]
    
    #only sent header if env variable set
    Header set X-Robots-Tag "noindex, follow" env=NOINDEXFOLLOW
    

    FilesMatch 适用于(本地)文件,而不是 url。所以它会尝试只匹配 url 的 /index.php 部分。 &lt;location&gt; 会更合适,但据我从文档中可以看出,这里不允许使用查询字符串。所以我最终得到了上述解决方案(我真的很喜欢这个挑战)。虽然 php 是放置这个的更明显的地方,但这取决于你。

    解决方案当然需要 mod_rewrite 和 mod_headers。

    【讨论】:

      【解决方案3】:

      请注意,您需要启用 mod_headers 模块才能设置标头。

      虽然像其他人说的那样,使用 php 标签似乎更好。不行吗?

      【讨论】:

        【解决方案4】:

        根据 Google 的说法,语法会有些不同:

        <Files ~ "\.pdf$">
          Header set X-Robots-Tag "noindex, nofollow"
        </Files>
        

        https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag

        【讨论】:

          猜你喜欢
          • 2017-10-05
          • 2020-08-06
          • 1970-01-01
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 2022-11-04
          • 2020-01-16
          • 1970-01-01
          相关资源
          最近更新 更多