PrePAN

Sign in to PrePAN

Plack::Middleware::Redirect Perform 301 redirects based on regexps

Good

Synopsis

    builder {
        enable 'Redirect', url_map => [
            '^/profile/batman$' => '/user/batman',
            '^/awe.*'           => '/awesome', 
            '^/dinosore/(.*)'   => '/dinosaur?angerlevel=$1',

            '^/bot(.*)'         => sub { 
                my ($env, $regex) = @_;

                my $path = $env->{PATH_INFO};
                $path =~ s|$regex|botulism/$1|;

                return $path;
            },
        ];
        $app;
    }

Description

This module performs a redirect (HTML status code 301) for urls which match a list of regular expressions. The redirect happens before $app is called which means $app is completely bypassed if a url matches.

Plack's PATH_INFO environment variable is the part of the url that looks like '/path/to/a/resource'.

The QUERY_STRING env var is the part of the url that looks like ?rubbery='octopus'&fluffy='bunny'.

Currently the regexps are only applied to PATH_INFO. The QUERY_STRING is always appended unaltered to the end of the new redirected url. I guess there should be a way to configure that. Or perhaps the regexp should apply against both PATH_INFO and QUERY_STRING.

Its a little weird that url_map is a Hash inside an ArrayRef. But when you have a list of 20 redirects, I feel the extra punctuation gets in the way of readability and there are several other Plack middleware modules that do this.

Comments

anonymouse
Anonymous
Should allow specifying one of 30{1,2,3,7} and maybe 300 as status code.
This is already provided by Plack::Middleware::Rewrite, a little less declaratively but much more flexibly.
Oh. Hate when that happens. Thanks ap.

Please sign up to post a review.