superbig/craft-countryredirect

根据访客的来源国家轻松重定向

安装数: 2,781

依赖: 0

建议者: 0

安全: 0

星标: 8

关注者: 2

分支: 2

公开问题: 19

类型:craft-plugin

2.2.1 2021-11-12 11:28 UTC

README

根据访客的来源国家轻松重定向到Craft网站

Screenshot

要求

此插件需要Craft CMS 3.0.0-beta.23或更高版本。

安装

要安装插件,请按照以下说明操作。

  1. 打开终端并转到您的Craft项目

     cd /path/to/project
    
  2. 然后让Composer加载插件

     composer require superbig/craft-countryredirect
    
  3. 在控制面板中,转到设置 → 插件,然后点击Country Redirect的“安装”按钮。或者,运行命令 ./craft install/plugin country-redirect

Country Redirect 概述

@TODO

使用插件

在开始使用插件之前,您需要做3件事

  1. 通过插件设置从MaxMind下载更新后的国家数据库

  2. 将配置文件 config.php 复制为 country-redirect.php 并放入Craft的 config 目录,通常为 config/

  3. 修改配置文件以匹配偏好设置,并确保您设置了 countryMap 设置以匹配您的站点名称。

  4. 添加您从 MaxMind.com License Key 获得的MaxMind.com 账户区域

配置

<?php
return [
    '*' => [
        /*
         * Enable for all users
         */
        //'enabled'             => true,
        /*
         * Or specifically logged in/anon users
         */
        'enabled'             => [
            'loggedIn'  => true,
            'anonymous' => true,
        ],

        /*
         * MaxMind.com License Key
         */
        'licenseKey' => null,

        /*
         * Enable logging
         */
        'enableLogging'          => true,

        /*
         * Don't redirect bots/crawlers like GoogleBot, Bing etc.
         */
        'ignoreBots'          => true,

        /*
        * Add any special URL patterns you want to ignore when deciding to redirect
        * This option accepts either exact matches (note the = prefix):
        * '=/shop',
        * or regex patterns:
        * '^\/global\//'
        */
        'ignoreUrlPatterns' => [
            // Matches '/shop' exactly
            '=/shop',

            // Matches uri that starts with '/global/'
            '/^\/global\//',

            // Matches a range of sitemap urls like '/sitemap.xml', '/no/sitemap.xml', '/sitemap_portfolio_1.xml', etc.
            '/^\/(no\/|en\/)*sitemap([\s\S])*\.xml$/',
        ],

        /*
         * Cookie name
         */
        'cookieName'          => 'countryRedirect',

        /*
         * The URL parameter that let a user manually select which locale they want to see
         */
        'overrideLocaleParam' => 'selected-locale',

        /*
         * The URL parameter that indicates that a user was redirect
         */
        'redirectedParam' => 'redirected',

        /*
        * The value of query params
        */
        'queryParamsValue' => '1',

        /*
         * Map a countrys two-letter ISO code to a Craft Site Handle, and/or define a catch-all with a * asterix
         * Here is a list of ISO country codes: http://www.nationsonline.org/oneworld/country_code_list.htm
         * Example:
         * 'countryMap'       => [
         *   'FR' => 'frenchSite',
         *   'DK' => 'danishSite',
         *   // You can also send visitors to an arbitrary URL
         *   'DE' => 'http://google.de',
         *   '*' => 'default',
         * ]
         *
         * If you within a country have different regional languages, you can map the different languages to sites by their Site Handle.
         * Take Switzerland, with German, French, Italian and Romansh, as an example:
         *
         * 'countryMap'       => [
         *   'CH' => [
         *     'fr' => 'frenchSite',
         *     'de' => 'germanSite',
         *   ],
         *   '*' => 'default',
         * ]
         *
         * This works by checking the Accept-Language header of the browser.
         *
         */
        'countryMap'          => [ ],

        /*
         * If you want to show a banner that prompts visitors to their matching site handle instead of redirecting them,
         * you can define these here.
         *
         * The key here is the Craft Site Handle, not the country code. The variables {countryName} and {url} will
         * be replaced.
         *
         * 'banners' => [
         *   'frenchSite' => 'It looks like your visiting from {countryName}. Do you <a href="{url}">want to visited the international site?</a>'
         * ],
         */
        'banners'             => [ ],

        /*
         * Override the detected IP - useful for testing, or when no IP address can be detected
         */
        'overrideIp'          => null,
    ],
];

有关ISO国家代码列表,请查看此概述

导航

<h1>Select country</h1>

{% set titles = {
    'default': 'Go to our US site',
    'dutchSite': 'Go to our Dutch site',
    'foreignSiteBranch': 'Go to our Danish site',
} %}
<nav class="nav">
    <ul>
        {% for link in craft.countryRedirect.getLinks() %}
            <li>{{link.getLink({ title: titles[link.getId()] })}}</li>
        {% endfor %}
    </ul>
</nav>

横幅提示

如果您想显示横幅提示访客查看其匹配的本地化页面而不是重定向他们,您可以使用banners设置定义每个本地化文本。

然后您可以这样访问文本

{% set banner = craft.countryRedirect.getBanner() %}
{% if banner %}
    <div class="banner">
        <p>{{ banner.getText() }}</p>
    </div>
{% endif %}

测试

您可以通过使用TunnelBear提供的免费VPN账户来测试它

数据库

MaxMind提供的地理位置数据

准确性

根据MaxMind,他们的数据库在国家级别上准确度为99.8%。

Superbig提供