定制 jorisnoo/craft-locale-redirect 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jorisnoo/craft-locale-redirect

最新稳定版本:1.0.2

Composer 安装命令:

composer require jorisnoo/craft-locale-redirect

包简介

Browser locale detection and redirect for Craft CMS

README 文档

README

A Craft CMS module that automatically redirects visitors from / to their locale-specific home route based on their browser language preferences.

Features

  • Detects the visitor's preferred language from the Accept-Language header
  • Matches it against your Craft multi-site locales
  • Redirects from / to the best-matching locale home URL (e.g. /en, /fr, /de)
  • Preserves query parameters through the redirect
  • Configurable locale exclusions and restrictions
  • Zero configuration required for basic usage

Requirements

  • PHP 8.2+
  • Craft CMS 5

Installation

composer require jorisnoo/craft-locale-redirect

Then register the module in your config/app.php:

return [
    'modules' => [
        'locale-redirect' => \Noo\CraftLocaleRedirect\Module::class,
    ],
    'bootstrap' => ['locale-redirect'],
];

How It Works

When a visitor hits your site's root URL (/), the module:

  1. Reads the Accept-Language header from the browser
  2. Fetches all configured Craft site locales and their URLs
  3. Finds the best match between browser preferences and available locales
  4. Issues a 302 redirect to the matched locale's home URL

If no match is found, the visitor is redirected to the primary site's URL (or a configured fallback). Query parameters are preserved through the redirect.

Configuration

The module works out of the box with no configuration. To customize behavior, create a config/locale-redirect.php file in your Craft project:

<?php

return [
    'excludeSites' => [],
    'exclude' => [],
    'only' => [],
    'fallback' => null,
];

Exclude Sites

Prevent the redirect from triggering on specific sites (by handle). Useful when locale-specific sites already have their own prefix and shouldn't redirect further:

'excludeSites' => ['english', 'german'],

Exclude Locales

Prevent specific locales from being redirect targets:

'exclude' => ['de', 'it'],

Restrict to Specific Locales

Only allow redirection to specific locales. Takes precedence over exclude when both are set:

'only' => ['en', 'fr'],

Fallback URL

The URL to redirect to when no browser locale matches. Defaults to the primary site URL:

'fallback' => '/en',

Branching and Versioning

This package follows Semantic Versioning. Only Craft 5 is supported.

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

  • 总下载量: 215
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固