--- /dev/null
+package Newslash::Plugin::Redirect;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::Util qw(md5_sum dumper);
+use List::Util qw(any);
+use Socket;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::Redirect - redirect control plugin for Newslash
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $app->plugin('Newslash::Plugin::Redirect');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::Redirect> controls redirection.
+
+
+=head1 METHODS
+
+=head2 register
+
+ $plugin->register(Mojolicious->new);
+
+Register hooks in L<Mojolicious> application.
+
+=cut
+
+sub register {
+ my ($self, $app, $conf) = @_;
+
+ $app->hook(around_dispatch => sub {
+ my ($next, $c) = @_;
+
+ # check subdomain redirection
+ my $hostname = ($c->config->{Site} || {})->{hostname} || "";
+ if ($hostname) {
+ my $request_host = $c->req->url->to_abs->host_port;
+ if ($request_host ne $hostname) {
+ my $subdomain = $request_host;
+ $subdomain =~ s/\.$hostname$//;
+
+ if ($subdomain) {
+ my $new_url = $c->req->url;
+ $new_url->host($hostname);
+ $c->res->code(301);
+ $c->redirect_to($new_url);
+ return;
+ }
+ }
+ }
+ $next->();
+ });
+}
+
+
+1;