locaine/lcn-x-robots-tag-bundle

轻松控制 X-Robots-Tag HTTP 头

安装: 23

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.0 2015-12-10 10:46 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:18:11 UTC


README

Build Status

轻松管理 X-Robots-Tag http 头(noindex, nofollow)在 Symfony2 中。

  • 允许您为 X-Robots-Tag 响应头定义一个默认值
  • 允许您为需要特定用户角色的请求定义 X-Robots-Tag 响应头的值
  • 允许您手动控制 X-Robots-Tag 响应头的值

安装

步骤 1:下载包

打开命令控制台,进入您的项目目录并执行以下命令以下载此包的最新稳定版本

$ composer require locaine/lcn-x-robots-tag-bundle "~1"

此命令要求您已全局安装 Composer,如 Composer 文档中的安装章节中所述。

步骤 2:启用包

然后,通过将其添加到项目中 app/AppKernel.php 文件中注册的包列表中来启用包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Lcn\XRobotsTagBundle\LcnXRobotsTagBundle(),
        );

        // ...
    }

    // ...
}

步骤 3:配置包

然后,在项目中 app/config.yml 文件中配置包

# app/config.yml
parameters:
    lcn_x_robots_tag.rules.default: { noindex: false, nofollow: false }
    
...

lcn_x_robots_tag:
    enabled: true
    rules:
        default: %lcn_x_robots_tag.rules.default%

高级配置选项

在控制器中显式控制 X-Robots-Tag 头值

假设您有一个产品列表页面,您希望搜索引擎爬虫跟随产品链接,但不要索引列表页面本身(例如,以避免重复内容)

class ProductController

    public function indexAction(Request $request)
    {
        $this->get('lcn_x_robots_tag')->setNoindex(true)->setNofollow(false);
        
        ...

XRobotsTag 服务上调用 setNoindexsetNofollow 将覆盖您在 app/config.yml 文件中定义的所有其他规则。

不索引需要特定用户角色的请求

如果您在 app/security.yml 中定义了用户角色和访问控制规则,则可以轻松地让搜索引擎爬虫不索引这些请求。如果您使用令牌(请参阅 Api Key Authenticator)登录或当 Http Basic Auth 用户凭据在 URL 中提供时,这很有用。如果您发送 403/401 状态头或将未认证的用户重定向到您的登录页面,这可能不太有用。

# app/config.yml
lcn_x_robots_tag:
    enabled: true
    rules:
        user_roles: true

上面的语法是简写表示法,等价于

# app/config.yml
lcn_x_robots_tag:
    enabled: true
    rules:
        user_roles:
            *: { noindex: true, nofollow: true }

您也可以只为某些用户角色应用规则

# app/config.yml
lcn_x_robots_tag:
    enabled: true
    rules:
        user_roles:
            ROLE_ADMIN: { noindex: true, nofollow: true }
            ROLE_EDITOR: { noindex: true, nofollow: true }

不索引开发环境

当然,您的开发环境不应公开访问,但如果它被访问,至少可以避免被索引

# app/config_dev.yml
imports:
    - { resource: config.yml }

parameters:
    lcn_x_robots_tag.rules.default: { noindex: true, nofollow: true }