承接 ctw/ctw-middleware-apex 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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

Latest Stable Version GitHub Actions Scrutinizer Build Scrutinizer Quality Code Coverage

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.com and www.example.com as different sites, potentially diluting page rank
  • Cookie scope: Cookies set on www.example.com are 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

  1. Duplicate content issues: Without canonicalization, search engines index the same content under multiple URLs
  2. Session inconsistencies: Cookies may not be shared between apex and www domains
  3. Certificate complexity: Some CDNs and hosting providers handle www subdomains more gracefully
  4. Manual redirect configuration: Eliminates the need to configure redirects at the web server level
  5. Environment-aware prefixes: Supports developer-specific prefixes (e.g., www-pl.example.com) via APP_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_ENV for developer-specific prefixes (e.g., development-pl creates www-pl.)
  • Production web applications: Ensure consistent canonical URLs across all requests

Design Goals

  1. Permanent redirects: Uses HTTP 301 status code for proper SEO handling
  2. Query string preservation: Maintains all query parameters during redirect
  3. Environment awareness: Supports custom prefixes via APP_ENV environment variable
  4. Transparent operation: Only redirects when necessary, passes through already-prefixed requests
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 ctw/ctw-middleware-apex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-03-09