Download the PHP package ctw/ctw-middleware-trailingslash without Composer

On this page you can find all versions of the php package ctw/ctw-middleware-trailingslash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package ctw-middleware-trailingslash

Package "ctw/ctw-middleware-trailingslash"

Latest Stable Version GitHub Actions Scrutinizer Build Scrutinizer Quality Code Coverage

PSR-15 middleware that enforces trailing slashes on URLs by redirecting with HTTP 301, ensuring consistent URL canonicalization across your application.

Introduction

Why This Library Exists

URLs with and without trailing slashes (e.g., /about vs /about/) are technically different resources. Without proper handling, the same content may be accessible at multiple URLs, causing SEO problems and potential caching issues.

This middleware enforces a consistent trailing slash convention by:

Problems This Library Solves

  1. Duplicate content: Search engines index both /page and /page/ as separate pages
  2. Inconsistent linking: Internal links may mix both formats
  3. Cache fragmentation: CDNs may cache the same page under multiple URLs
  4. Relative URL issues: Browser relative URL resolution differs based on trailing slash
  5. Manual redirects: Maintaining nginx/Apache redirect rules is error-prone

Where to Use This Library

Design Goals

  1. Permanent redirects: Uses HTTP 301 for SEO benefit
  2. Smart file detection: Skips URLs with file extensions automatically
  3. Configurable exclusions: Disable for specific paths like /api/
  4. Early redirect: Redirects before processing the request (no wasted computation)
  5. Query string preservation: Full URI preserved during redirect

Requirements

Installation

Install by adding the package as a Composer requirement:

Usage Examples

Basic Pipeline Registration (Mezzio)

ConfigProvider Registration

Redirect Behavior

Request URL Result Status
/about Redirect to /about/ 301
/about/ Pass through -
/page?id=1 Redirect to /page/?id=1 301
/style.css Pass through (has extension) -
/image.png Pass through (has extension) -
/api/users Pass through (if excluded) -
/ Pass through (root) -

Path Exclusions

Exclude specific paths from trailing slash processing:

File Extension Handling

The middleware automatically detects file extensions and skips processing:

Query String Preservation

Query strings are preserved during redirect:

Response Header

Testing with cURL

Nginx Equivalent

This middleware replaces the need for nginx rewrite rules like:

Why Trailing Slashes Matter

  1. SEO: Google treats /page and /page/ as different URLs
  2. Relative links: <a href="sub"> resolves differently based on trailing slash
  3. Caching: CDNs may cache multiple versions of the same page
  4. Analytics: Traffic may be split across URL variations

All versions of ctw-middleware-trailingslash with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
ctw/ctw-http Version ^4.0
ctw/ctw-middleware Version ^4.0
psr/container Version ^1.0 || ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ctw/ctw-middleware-trailingslash contains the following files

Loading the files please wait ...