in2code/ipandlanguagesuggest

该包已被废弃,不再维护。作者建议使用 in2code/ipandlanguageredirect 包。

自动或通过suggestlink重定向TYPO3访客到另一种语言和/或根页面。

安装: 1 187

依赖项: 0

建议者: 0

安全: 0

星标: 14

关注者: 9

分支: 14

公开问题: 7

类型:typo3-cms-extension

5.2.1 2024-04-23 09:09 UTC

README

TYPO3 FE访客自动或手动重定向到另一个语言或另一个根页面。

简介

此扩展允许通过基于访客浏览器语言和地区(IP地址)的重定向到最佳匹配页面来处理多语言和多域名。

AJAX请求(仅VanillaJS - 无需JavaScript框架)将处理服务器端域逻辑以重定向或建议新的网页或新的语言。

在您的PHP配置中定义哪些国家属于哪个页面树以及哪种浏览器语言属于哪种前端语言

简而言之

自动重定向或显示提示,为访客提供最适合其需求的网站版本。

屏幕截图

前端示例提示信息:建议语言

如何获取用户的国家代码?

这完全由您选择。您可以选择不同的ip2country服务类。一方面,有一个包含IP范围和国家的地方表,另一方面,您还可以使用外部服务将访客的IP转换为countryCode。第一种方法尊重所有隐私,第二种方法当然更加及时。

或者:您可以组合不同的方法:因为IpApi的免费服务每月有1000个请求,您可以使用这个,并附加离线表变体。

默认可用类

  • In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpApi - 使用ipapi.co的外部服务将IP地址转换为国家代码。注意:如果您想每月使用超过1000次请求,可以通过TypoScript设置API密钥
  • In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\LocalDatabase - 使用本地数据库表 tx_ipandlanguageredirect_domain_model_iptocountry 将IP地址转换为国家 - 无外部服务,最大隐私
  • In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpApiCom - 使用ip-api.com的外部服务将IP地址转换为国家代码。注意:因为这个服务不支持https,所有请求都通过端口80处理

转到扩展管理器设置,选择您要使用的类,或者简单地添加您自己的服务。扩展管理器设置中ipToCountryService的示例字符串可以是 In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpApi,In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\LocalDatabase 以使用IpApi的服务,只要它是免费的,然后回退到本地数据库。

与其他扩展如rmpl_language_detect有什么区别?

在概念上有一个基本区别。虽然大多数语言重定向扩展通过USER_INT钩入页面渲染过程,我们选择使用JavaScript和PHP(AJAX)的异步方式。这种解决方案需要JavaScript,但另一方面,对于高可用性和更复杂的网站来说,速度要快得多。这意味着:您可以使用例如 staticfilecache 或其他静态解决方案来提高Web性能。当USER_INT包含在每一页时,无法使用staticfilecache。

测试!

此扩展允许您测试如果来自不同国家的访客检查您的网站时,您的网站将如何反应。下面有大量的测试选项。

安装

  • 安装此扩展
  • 添加一个静态模板(page.5将用于建议容器,page.1555用于脚本)
  • 将ipandlanguageredirect/Configuration/Redirect/Redirect.php中的原始配置复制到任何其他位置
  • 根据需要修改配置
  • 在扩展管理器中设置新路径到您的配置文件
  • 祝您玩得开心!

示例配置

<?php
return [
    // Example action
    'actions' => [
        [
            // Automatic redirect to best fitting uri if visitor comes from a search engine
            'referrers' => [
                'google.',
                'bing.',
                'yahoo.',
                't-online.',
                'yandex.',
                'baidu.'
            ],
            'events' => [
                'redirect'
            ]
        ],
        [
            // For all other cases: Suggest a better fitting page
            'referrers' => [
                '*'
            ],
            'events' => [
                'suggest'
            ]
        ],
        [
            // Disable redirect for pages inside the given pid
            'pidInRootline' => [
                '129',
                '11',
            ],
            'events' => [
                'none',
            ],
        ],
        [
            // Prevent redirect for google pagespeed access
            'userAgent' => [
                'Chrome-Lighthouse'
            ],
            'events' => [
                'none'
            ]
        ],
    ],
    'globalConfiguration' => [
        // don't show suggest or redirect on a subpage
        'actionOnHomeOnly' => false,

        // always redirect to the home-page, don not try to stay on the same page while changing the language
        'stayOnCurrentPage' => false
    ],
    // configuration if nothing matches
    'noMatchingConfiguration' => [
        'identifierUsage' => 'worldwide_english',
        'matchMinQuantifier' => 15
    ],
    // main redirect configuration
    'redirectConfiguration' => [

        // Build URI to page 1 if visitors came from anywhere in the world
        1 => [

            // Build URI to language 0 if browser language is not defined here
            0 => [
                'identifier' => 'worldwide_english',
                'domain' => [
                    'www.domain.com',
                    'test.domain.org'
                ],
                'browserLanguage' => [
                    '*'
                ],
                'countryBasedOnIp' => [
                    '*'
                ]
            ],

            // Build URI to language 1 if browser language is german "de"
            1 => [
                'identifier' => 'worldwide_german',
                'domain' => [
                    'www.domain.com',
                    'test.domain.org'
                ],
                'browserLanguage' => [
                    'de'
                ],
                'countryBasedOnIp' => [
                    '*'
                ]
            ],

            // Build URI to language 2 if browser language is chinese "zh"
            2 => [
                'identifier' => 'worldwide_chinese',
                'domain' => [
                    'www.domain.com',
                    'test.domain.org'
                ],
                'browserLanguage' => [
                    'zh'
                ],
                'countryBasedOnIp' => [
                    '*'
                ]
            ],
        ],

        // Build URI to page 2 if visitors came from USA, Canada or Argentina
        2 => [

            // Build URI to language 0 if browser language is not defined here
            0 => [
                'identifier' => 'america_english',
                'domain' => [
                    'www.seconddomain.org',
                    'test.seconddomain.org'
                ],
                'browserLanguage' => [
                    '*'
                ],
                'countryBasedOnIp' => [
                    'ca',
                    'us',
                    'ar',
                ]
            ],

            // Build URI to language 1 if browser language is german "de"
            1 => [
                'identifier' => 'worldwide_german',
                'domain' => [
                    'www.seconddomain.org',
                    'test.seconddomain.org'
                ],
                'browserLanguage' => [
                    'de'
                ],
                'countryBasedOnIp' => [
                    '*'
                ]
            ],
        ],
    ],
    // Quantifiers for the matches (shouldn't be touched)
    'quantifier' => [
        'browserLanguage' => [
            'totalMatch' => 7,
            'wildCardMatch' => 3
        ],
        'countryBasedOnIp' => [
            'totalMatch' => 13,
            'wildCardMatch' => 5
        ],
        'domain' => [
            'totalMatch' => 10,
            'wildCardMatch' => 4
        ],
        'actions' => [
            'referrers' => [
                'totalMatch' => 7,
                'wildCardMatch' => 3,
            ],
            'userAgents' => [
                'totalMatch' => 7,
                'wildCardMatch' => 3,
            ],
            'pidInRootline' => 9999
        ]
    ]
];

TypoScript

如果您添加了静态TypoScript,这些行将产生效果

plugin.tx_ipandlanguageredirect {
	view {
		templateRootPaths {
			0 = EXT:ipandlanguageredirect/Resources/Private/Templates/
			1 = {$plugin.tx_ipandlanguageredirect.view.templateRootPath}
		}
	}
	features.requireCHashArgumentForActionArguments = 0

	settings {
		# Add configuration to your IpToCountry service classes
		ipToCountry {
			In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpApi {
				# IpApi Key: Please enter your key for ipapi.co (optional), otherwise extension will have limited access to the service (less then 1000 visitors a day). See ipapi.co for details.
				ipApiKey =
			}
		}
	}
}

page {
	includeJSFooter.ipandlanguageredirect = EXT:ipandlanguageredirect/Resources/Public/JavaScripts/Frontend.min.js
	includeCSS.ipandlanguageredirect = EXT:ipandlanguageredirect/Resources/Public/Css/Frontend.min.css

	# Suggest container that can be slided down
	5 = USER
	5 {
		userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
		extensionName = Ipandlanguageredirect
		pluginName = Pi1
		vendorName = In2code
		controller = Redirect
		action = suggest
		switchableControllerActions.Redirect.1 = suggest
	}

	# Container for informations that will be send to an AJAX service
	1555 = COA
	1555 {
		wrap = <script id="ipandlanguageredirect_container"|></script>

		# Uri to send AJAX request to
		10 = TEXT
		10 {
			noTrimWrap = | data-ipandlanguageredirect-ajaxuri="|"|
			typolink {
				parameter.data = TSFE:id
				additionalParams = &type=1555
				returnLast = url
				forceAbsoluteUrl = 1
			}
		}

		# FE language
		20 = TEXT
		20 {
			noTrimWrap = | data-ipandlanguageredirect-languageuid="|"|
			data = sitelanguage:languageId
			ifEmpty.data = GP:L
			intval = 1
		}

		# Root page uid
		30 = TEXT
		30 {
			noTrimWrap = | data-ipandlanguageredirect-rootpageuid="|"|
			data = leveluid:0
		}

		# Fake browser language for testing - e.g. &tx_ipandlanguageredirect_pi1[browserLanguage]=en
		40 = TEXT
		40 {
			noTrimWrap = | data-ipandlanguageredirect-browserlanguage="|"|
			data = GP:tx_ipandlanguageredirect_pi1|browserLanguage
			htmlSpecialChars = 1
			required = 1
		}

		# Fake ip-address for testing - e.g. &tx_ipandlanguageredirect_pi1[ipAddress]=66.85.131.18 (USA)
		50 = TEXT
		50 {
			noTrimWrap = | data-ipandlanguageredirect-ipaddress="|"|
			data = GP:tx_ipandlanguageredirect_pi1|ipAddress
			htmlSpecialChars = 1
			required = 1
		}

		# Fake country for testing (overlays ip-address) - e.g. &tx_ipandlanguageredirect_pi1[countryCode]=us (USA)
		60 = TEXT
		60 {
			noTrimWrap = | data-ipandlanguageredirect-countrycode="|"|
			data = GP:tx_ipandlanguageredirect_pi1|countryCode
			htmlSpecialChars = 1
			required = 1
		}

		# Fake referrer for testing - e.g. &tx_ipandlanguageredirect_pi1[referrer]=www.google.de
		70 = TEXT
		70 {
			noTrimWrap = | data-ipandlanguageredirect-referrer="|"|
			data = GP:tx_ipandlanguageredirect_pi1|referrer
			htmlSpecialChars = 1
			required = 1
		}

		# Fake domain for testing - e.g. &tx_ipandlanguageredirect_pi1[domain]=www.production.org
		80 = TEXT
		80 {
			noTrimWrap = | data-ipandlanguageredirect-domain="|"|
			data = GP:tx_ipandlanguageredirect_pi1|domain
			htmlSpecialChars = 1
			required = 1
		}
	}
}

# AJAX types
redirectAjax = PAGE
redirectAjax {
	typeNum = 1555
	config {
		additionalHeaders = Content-Type: application/json
		additionalHeaders.10.header = Content-Type: application/json
		no_cache = 1
		disableAllHeaderCode = 1
		disablePrefixComment = 1
		xhtml_cleaning = 0
		admPanel = 0
		debug = 0
	}

	10  = USER
	10 {
		userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
		extensionName = Ipandlanguageredirect
		pluginName = Pi1
		vendorName = In2code
		controller = Redirect
		action = redirect
		switchableControllerActions.Redirect.1 = redirect
	}
}
testAjax < redirectAjax
testAjax {
	typeNum = 1556
	10.action = test
	10.switchableControllerActions.Redirect.1 = test
}

测试

模拟浏览器语言、国家和引用者

http://domain.org/index.php?id=1
&tx_ipandlanguageredirect_pi1[ipAddress]=66.85.131.18
&tx_ipandlanguageredirect_pi1[browserLanguage]=de
&tx_ipandlanguageredirect_pi1[referrer]=www.google.de
&tx_ipandlanguageredirect_pi1[countryCode]=af
&tx_ipandlanguageredirect_pi1[domain]=www.domain.org
&no_cache=1

注意:请注意,这些设置默认情况下会被缓存。因此,您必须始终添加一个 &no_cache=1

不进行重定向或建议,但在浏览器控制台显示重定向Uri

http://domain.org/index.php?id=1
&ipandlanguagedebug=1

来自您的服务器的示例响应

{
  "redirectUri": "https:\/\/local.domain.org\/de\/",
  "activated": true,
  "events": [
    "redirect"
  ],
  "activatedReasons": {
    "differentLanguages": true,
    "differentRootpages": false
  },
  "country": "jp",
  "givenParameters": {
    "browserLanguage": "de",
    "referrer": "www.google.de",
    "ipAddress": "27.121.255.4",
    "languageUid": 0,
    "rootpageUid": 209,
    "countryCodeOverlay": "",
    "domain": "local.domain.org"
  }
}

常见问题解答

  • 隐藏建议信息
    • Q1:如何在点击我的链接之一时禁用建议信息?
    • A1:您可以在页面的任何位置添加具有属性 data-ipandlanguageredirect-action="hideMessage" 的链接或其他dom元素来禁用建议信息。用例:如果用户手动更改您的网站上的区域或语言,则信息应在将来消失。
    • Q2:如何使用GET参数禁用建议信息?
    • A2:如果提供GET参数 &h=1&h=3,则消息将被隐藏,并将设置隐藏消息的cookie。
  • 禁用重定向
    • Q1:如何在点击我的链接之一时禁用重定向?
    • A1:您可以在页面的任何位置添加具有属性 data-ipandlanguageredirect-action="disableRedirect" 的链接或其他dom元素来禁用重定向。
    • Q2:如何使用GET参数禁用重定向?
    • A2:如果提供GET参数 &h=2&h=3,则通过设置cookie禁用重定向。
  • Cookie有效期
    • 默认cookie有效期是临时的——只要浏览器打开。目前无法调整。
  • 测试
    • Q1:如何测试另一个IP地址、区域或浏览器语言?
    • A1:请参阅测试部分
    • Q2:在哪里可以看到通过AJAX发送和接收的哪些参数?
    • A2:打开您的浏览器控制台并检查到 ?type=1555 的post请求——检查参数或响应
  • Typenum将不起作用
    • Q1:在TYPO3 9或更高版本中,我的请求不起作用——我能做什么?
    • Q1:您必须在您的路由配置中定义使用的type参数(请参阅以下示例)

路由示例

routeEnhancers:
    PageTypeSuffix:
        type: PageType
        map:
            redirect.json: 1555

您的贡献

欢迎提交 pull requests!但请记住,不要忘记在pull requests中添加描述。这非常有帮助,可以了解PR将解决什么问题。

  • 错误修复:请描述您的修复解决了哪种类型的错误,并告诉我如何重现问题。只有当我能够重现问题时,我才会接受错误修复。
  • 功能:并非每个功能都对大量扩展用户都相关。请在讨论新功能之前进行讨论。

使用ddev的贡献

要求

  1. 安装ddev,请参阅:https://ddev.readthedocs.io/en/stable/#installation
  2. 安装git-lfs,请参阅: https://git-lfs.github.com/

安装

  1. 克隆此存储库
  2. 运行 ddev start
  3. 运行 ddev initialize 以设置配置和测试数据库

目前(尚未)无法实现的功能

  • 在浏览器语言中使通配符使用可用(例如,“en*”用于所有英语浏览器语言)
  • 支持所有浏览器语言,而不仅仅是第一个
  • 也支持大洲

变更日志

版本 日期 状态 描述 注意
5.2.1 2023-04-23 错误修复 正确约束的错误修复
5.2.0 2023-04-20 特性 支持TYPO3 12
5.1.2 2023-06-12 安全 修复可能的SQL注入
5.1.1 2023-02-07 错误修复 修复PHP 8中的未定义数组键异常
5.1.0 2022-07-12 任务 Css和前端工具链更新
5.0.0 2022-04-08 任务 对TYPO3 11的更新(现在已放弃对9和10的支持)
4.0.1 2021-11-22 任务 在composer.json文件中定义扩展密钥
4.0.0 2020-12-31 任务 对TYPO3 9和10的更新
3.1.0 2020-02-07 任务 小更新:将ext图标移动到Public,清理composer.json,更新文档
3.0.1 2019-08-06 错误修复 不要使用错误的根页面的配置
3.0.0 !!! 2019-07-05 特性 基于用户代理执行操作(禁用google页面速度的重定向) 更新您的配置
2.2.0 2019-04-26 任务 支持TYPO3 9的语言处理,小文档修复
2.1.0 2019-01-10 任务 小清理,小文本更改
2.0.0 !!! 2018-10-18 特性 多域名处理,actionOnHomeOnly,离线ip2geo,stayOnCurrentPage 更新您的配置
1.8.0 2018-08-23 任务 添加扩展图标,添加一些文档
1.7.2 2018-01-21 任务 现在允许ipapi密钥,无需&key=
1.7.1 2018-01-16 错误修复 如果没有给出测试值,则不要为IP地址值发送“null”
1.7.0 2017-09-25 特性 支持每天超过1000访问者的IpApi.co的付费版本
1.6.4 2017-08-11 错误修复 修复TYPO3 8中的头输出
1.6.3 2017-07-28 错误修复 在T3 8中禁用AJAX请求的cHash检查
1.6.2 2017-07-12 错误修复 修复composer.json中的小错别字
1.6.1 2017-04-12 错误修复 修复从chrome获取浏览器语言
1.6.0 2017-02-04 特性 会话中重定向应仅发生一次
1.5.3 2017-02-01 错误修复 使代码对PHP 5.5可读
1.5.2 2017-01-31 任务 删除未使用的typoscript
1.5.1 2017-01-26 错误修复 如果state == none,则避免JS异常
1.5.0 2017-01-25 特性 添加到页面树禁用功能的选项
1.4.1 2016-11-15 任务 对HTML和CSS进行语义代码清理
1.4.0 2016-11-15 特性 对HTML和CSS进行语义代码清理
1.3.0 2016-11-07 特性 在GET参数上隐藏建议消息
1.2.0 2016-11-03 特性 现在允许在多链接上hideMessage
1.1.0 2016-11-01 特性 添加测试功能
1.0.0 2016-11-01 初始 初始版本