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.
Installs: 118
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ctw/ctw-middleware-apex
Requires
- php: ^8.3
- ctw/ctw-http: ^4.0
- ctw/ctw-middleware: ^4.0
- psr/container: ^1.0 || ^2.0
Requires (Dev)
- ctw/ctw-qa: ^5.0
- phpunit/phpunit: ^12.0
- symfony/var-dumper: ^7.0
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, ];