Skip to content

Instantly share code, notes, and snippets.

@mche
Last active January 9, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mche/a5670e5fd5b932409d184ea718a1581b to your computer and use it in GitHub Desktop.
Save mche/a5670e5fd5b932409d184ea718a1581b to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
=pod
Usage:
perl script/redirect.pl daemon --listen http://*:80
Or uncomment listen option below and
perl script/redirect.pl daemon
=cut
my $conf = {
listen => 'http://*:80',
scheme => 'https',
#~ port => '443', !!! https scheme not need
host => 'mydomain.com',
code=> '301',# Moved code status
server => 'Redirector 0.01',
mode => 'production',
};
push @ARGV, '--listen', $conf->{listen}
if $conf->{listen};
hook before_dispatch => sub {
my $c = shift;
my $url = $c->req->url->to_abs;
$url->can($_) && $url->$_($conf->{$_})
for keys %$conf;
#~ warn $url->to_string if $url->userinfo;
$c->tx->res->headers->server($conf->{server});
$c->res->code($conf->{code});
$c->redirect_to($url);
};
app->mode($conf->{mode})->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment