| 1 | package Bootylicious::Plugin::CanonicalUrl; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Mojolicious::Plugin'; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my ($self, $app, $conf) = @_; | ||
| 10 | |||
| 11 | main::under(\&_bridge); | ||
| 12 | } | ||
| 13 | |||
| 14 | sub _bridge { | ||
| 15 | my $self = shift; | ||
| 16 | |||
| 17 | my $path = $self->req->url->path->to_string; | ||
| 18 | return 1 unless $path; | ||
| 19 | |||
| 20 | return 1 if $path =~ m{/$}; | ||
| 21 | |||
| 22 | return 1 if $path =~ m{\.(?:[a-z]+)$}; | ||
| 23 | |||
| 24 | my $canonical_location = | ||
| 25 | $self->req->url->clone->path($path . '.html')->to_abs; | ||
| 26 | |||
| 27 | $self->app->log->debug("Path is not canonical: " . $self->req->url); | ||
| 28 | $self->app->log->debug("Redirecting to: " . $canonical_location); | ||
| 29 | |||
| 30 | $self->redirect_to($canonical_location); | ||
| 31 | |||
| 32 | # Stop | ||
| 33 | return 0; | ||
| 34 | } | ||
| 35 | |||
| 36 | 1; |