PHPackages                             ctw/ctw-middleware-apex - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. ctw/ctw-middleware-apex

ActiveLibrary[HTTP &amp; Networking](/categories/http)

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.

4.0.4(5mo ago)1125BSD-3-ClausePHPPHP ^8.3CI passing

Since Mar 9Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jonathanmaron/ctw-middleware-apex)[ Packagist](https://packagist.org/packages/ctw/ctw-middleware-apex)[ RSS](/packages/ctw-ctw-middleware-apex/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (24)Used By (0)

Package "ctw/ctw-middleware-apex"
=================================

[](#package-ctwctw-middleware-apex)

[![Latest Stable Version](https://camo.githubusercontent.com/c57c5d892bb5e71c65a533407bdaedf934afa62125b637937582f4126e972614/68747470733a2f2f706f7365722e707567782e6f72672f6374772f6374772d6d6964646c65776172652d617065782f762f737461626c65)](https://packagist.org/packages/ctw/ctw-middleware-apex)[![GitHub Actions](https://github.com/jonathanmaron/ctw-middleware-apex/actions/workflows/tests.yml/badge.svg)](https://github.com/jonathanmaron/ctw-middleware-apex/actions/workflows/tests.yml)[![Scrutinizer Build](https://camo.githubusercontent.com/30944a76943dd056fd31b4a53d138e90f568fca58c31b768ecfcc757e3450e9c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d617065782f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-apex/build-status/master)[![Scrutinizer Quality](https://camo.githubusercontent.com/37223ecd66e6e1f588ff0702f609cb548e5926fa28809d6840da65be99a2cc49/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d617065782f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-apex/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/687e594e3a9d6eec5010759c322866ef089f467151b3e2b008a3884a292fdb99/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f6e617468616e6d61726f6e2f6374772d6d6964646c65776172652d617065782f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jonathanmaron/ctw-middleware-apex/?branch=master)

PSR-15 middleware that redirects apex (bare) domains to www-prefixed domains using HTTP 301 permanent redirects.

Introduction
------------

[](#introduction)

### Why This Library Exists

[](#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

[](#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

[](#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

[](#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
------------

[](#requirements)

- PHP 8.3 or higher
- ctw/ctw-middleware ^4.0
- ctw/ctw-http ^4.0

Installation
------------

[](#installation)

Install by adding the package as a [Composer](https://getcomposer.org) requirement:

```
composer require ctw/ctw-middleware-apex
```

Usage Examples
--------------

[](#usage-examples)

### Basic Pipeline Registration (Mezzio)

[](#basic-pipeline-registration-mezzio)

```
use Ctw\Middleware\ApexMiddleware\ApexMiddleware;

// In config/pipeline.php or similar
$app->pipe(ApexMiddleware::class);
```

### Redirect Behavior

[](#redirect-behavior)

Request URLRedirect URLStatus`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

[](#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 URLRedirect URL`http://example.com/``http://www-pl.example.com/`This enables multiple developers to work on the same domain with isolated environments.

### ConfigProvider Registration

[](#configprovider-registration)

The package includes a `ConfigProvider` for automatic factory registration:

```
// config/config.php
return [
    // ...
    \Ctw\Middleware\ApexMiddleware\ConfigProvider::class,
];
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance71

Regular maintenance activity

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~131 days

Total

23

Last Release

166d ago

Major Versions

3.0.16 → 4.0.02024-06-18

PHP version history (3 changes)3.0.0PHP ^8.0

3.0.3PHP ^8.1

4.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/18d8bc9bdee8ab1b0cfccce6a06d2438acbed3a58b0046c4834c5678f6769342?d=identicon)[jonathanmaron](/maintainers/jonathanmaron)

---

Top Contributors

[![jonathanmaron](https://avatars.githubusercontent.com/u/298462?v=4)](https://github.com/jonathanmaron "jonathanmaron (31 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ctw-ctw-middleware-apex/health.svg)

```
[![Health](https://phpackages.com/badges/ctw-ctw-middleware-apex/health.svg)](https://phpackages.com/packages/ctw-ctw-middleware-apex)
```

###  Alternatives

[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.3k63.6M232](/packages/nelmio-api-doc-bundle)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k48.1M236](/packages/api-platform-core)[api-platform/state

API Platform state interfaces

223.4M57](/packages/api-platform-state)[akrabat/ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

1702.5M18](/packages/akrabat-ip-address-middleware)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
