Skip to content

Instantly share code, notes, and snippets.

@robhammond
Forked from Logioniz/redirect.pl
Last active February 16, 2018 15:43
Show Gist options
  • Save robhammond/6936cf6df0e0244e4da48399b216bf1c to your computer and use it in GitHub Desktop.
Save robhammond/6936cf6df0e0244e4da48399b216bf1c to your computer and use it in GitHub Desktop.
Redirect from http to https with mojolicious
#!/usr/bin/perl
# To run need listen two ports: 80, 443
# sudo morbo 12.pl -l https://*:443?cert%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.crt%26key%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.key%26verify%3D0x00 -l http://*:80
# go to url in browser http://your_ip/qwe/asd
use Mojo::Base -strict;
use Mojolicious::Lite;
app->hook(before_dispatch => sub {
my $c = shift;
my $request_url = $c->req->url->to_abs;
if ($request_url->scheme eq 'http') {
$request_url->scheme('https');
my $u = $request_url->to_string;
$c->redirect_to($u);
}
});
get '/qwe/asd' => sub { shift->render(text => 'Hello, world') };
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment