ctw/ctw-middleware-apex
Composer 安装命令:
composer require ctw/ctw-middleware-apex
包简介
This PSR-15 middleware redirects with an HTTP 301 ("Moved Permanently") Location header an apex domain to www e.g. example.com to www.example.com.
README 文档
README
PSR-15 middleware that redirects apex (bare) domains to www-prefixed domains using HTTP 301 permanent redirects.
Introduction
Why This Library Exists
Many web applications need to canonicalize their URLs by redirecting apex domains (e.g., example.com) to their www-prefixed equivalents (e.g., www.example.com). This is important for:
- SEO consistency: Search engines treat
example.comandwww.example.comas different sites, potentially diluting page rank - Cookie scope: Cookies set on
www.example.comare more restrictive than those on the apex domain - CDN and DNS flexibility: The www subdomain allows CNAME records, while apex domains typically require A records
- Load balancing: Subdomains provide more flexibility for DNS-based traffic distribution
This middleware handles the redirect automatically at the application layer, ensuring all requests arrive at the canonical www-prefixed domain.
Problems This Library Solves
- Duplicate content issues: Without canonicalization, search engines index the same content under multiple URLs
- Session inconsistencies: Cookies may not be shared between apex and www domains
- Certificate complexity: Some CDNs and hosting providers handle www subdomains more gracefully
- Manual redirect configuration: Eliminates the need to configure redirects at the web server level
- Environment-aware prefixes: Supports developer-specific prefixes (e.g.,
www-pl.example.com) viaAPP_ENV
Where to Use This Library
- Mezzio applications: Add to your middleware pipeline early in the request lifecycle
- PSR-15 compatible frameworks: Any framework supporting PSR-15 middleware
- Multi-environment deployments: Use
APP_ENVfor developer-specific prefixes (e.g.,development-plcreateswww-pl.) - Production web applications: Ensure consistent canonical URLs across all requests
Design Goals
- Permanent redirects: Uses HTTP 301 status code for proper SEO handling
- Query string preservation: Maintains all query parameters during redirect
- Environment awareness: Supports custom prefixes via
APP_ENVenvironment variable - Transparent operation: Only redirects when necessary, passes through already-prefixed requests
- Zero configuration: Works out of the box with sensible defaults
Requirements
- PHP 8.3 or higher
- ctw/ctw-middleware ^4.0
- ctw/ctw-http ^4.0
Installation
Install by adding the package as a Composer requirement:
composer require ctw/ctw-middleware-apex
Usage Examples
Basic Pipeline Registration (Mezzio)
use Ctw\Middleware\ApexMiddleware\ApexMiddleware; // In config/pipeline.php or similar $app->pipe(ApexMiddleware::class);
Redirect Behavior
| Request URL | Redirect URL | Status |
|---|---|---|
http://example.com/ |
http://www.example.com/ |
301 |
http://example.com/page?id=1 |
http://www.example.com/page?id=1 |
301 |
https://example.com/path |
https://www.example.com/path |
301 |
http://www.example.com/ |
(no redirect) | - |
http://www-pl.example.com/ |
(no redirect) | - |
Environment-Aware Prefixes
When APP_ENV contains a two-letter suffix separated by a hyphen (e.g., development-pl), the middleware uses that as a developer-specific prefix:
# Environment variable
APP_ENV=development-pl
| Request URL | Redirect URL |
|---|---|
http://example.com/ |
http://www-pl.example.com/ |
This enables multiple developers to work on the same domain with isolated environments.
ConfigProvider Registration
The package includes a ConfigProvider for automatic factory registration:
// config/config.php return [ // ... \Ctw\Middleware\ApexMiddleware\ConfigProvider::class, ];
ctw/ctw-middleware-apex 适用场景与选型建议
ctw/ctw-middleware-apex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ctw/ctw-middleware-apex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ctw/ctw-middleware-apex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 130
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-03-09