NAME

PAGI::Middleware::Rewrite - URL rewriting middleware

SYNOPSIS

use PAGI::Middleware::Builder;

my $app = builder {
    enable 'Rewrite',
        rules => [
            { from => qr{^/old/(.*)}, to => '/new/$1' },
            { from => '/legacy', to => '/modern' },
        ];
    $my_app;
};

DESCRIPTION

PAGI::Middleware::Rewrite rewrites request paths before passing to the inner application. Supports both exact matches and regex patterns.

CONFIGURATION

  • rules (required)

    Arrayref of rewrite rules. Each rule is a hashref with:

    { from => '/old-path', to => '/new-path' }
    { from => qr{^/user/(\d+)}, to => '/users/$1' }
  • redirect (default: 0)

    If true, send redirect response instead of rewriting internally.

  • redirect_code (default: 301)

    HTTP status code for redirects.

REWRITE PATTERNS

Regex patterns can use capture groups:

{ from => qr{^/blog/(\d{4})/(\d{2})}, to => '/archive/$1-$2' }

This would rewrite /blog/2024/01 to /archive/2024-01.

SEE ALSO

PAGI::Middleware - Base class for middleware

PAGI::Middleware::HTTPSRedirect - Force HTTPS redirects