TrailingSlash class

Middleware that normalizes trailing slashes in URLs.

This middleware handles trailing slashes by either removing or appending them, with an optional redirect.

Example

// Remove trailing slashes: /about/ → /about
app.use(trimTrailingSlash());

// Append trailing slashes: /about → /about/
app.use(appendTrailingSlash());

Behavior

  • Only applies to GET and HEAD requests
  • By default, issues a 301 redirect
  • Preserves query strings during redirect

See also

Implemented types

Constructors

TrailingSlash({required TrailingSlashMode mode, int redirectStatus = HttpStatus.movedPermanently})
Creates a TrailingSlash middleware.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
mode TrailingSlashMode
The mode of operation.
final
redirectStatus int
The HTTP status code for redirects (default: 301).
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

handle(Context ctx, NextFunction next) FutureOr<void>
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

append({int redirectStatus = HttpStatus.movedPermanently}) TrailingSlash
Creates a middleware that appends trailing slashes.
trim({int redirectStatus = HttpStatus.movedPermanently}) TrailingSlash
Creates a middleware that removes trailing slashes.