The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojolicious::Plugin::RedirectHost - Redirects requests from mirrors to the main host (useful for SEO)

VERSION

version 1.04

SYNOPSIS

Generates 301 redirect from http://mirror.main.host/path?query to http://main.host/path?query

  # Mojolicious
  $app->plugin('RedirectHost', host => 'main.host');
  
  # Mojolicious::Lite
  plugin RedirectHost => { host => 'main.host' };

All requests with Host header that is not equal to the host option will be redirected to the main host

        www.main.host       => main.host
        another.io/foo?bar  => main.host/foo?bar
        etc...

You can point as many domains to your App by DNS, as you want. It doesn't matter, all of them will become a mirror. An equivalent apache .htaccess file looks like

        RewriteCond %{HTTP_HOST}   !^alexbyk.com
        RewriteRule  ^(.*)              http://alexbyk.com/$1 [R=301,L]

It would be better if you'll be using per mode config files (your_app.production.conf etc). This would make possible to redirect only in production enviropment (but do nothing while coding your app)

OPTIONS/USAGE

host

Main domain. All requests to the mirrors will be redirected to the host (domain) This option is required. Without it plugin do nothing

code

  $app->plugin('RedirectHost', host => 'main.host', code => 302);

Type of redirection. Default 301 (Moved Permanently)

er (except /robots.txt)

  $app->plugin('RedirectHost', host => 'main.host', er => 1);

If true, requests like /robots.txt will not be redirected but rendered. That's for Yandex search engine. If you want to change a domain but worry about yandex TIC, it's recomended to make it possible for Yandex to read your robots.txt with new Host directive. If so, that's exactly what you're looking for

silent

If silent is true, doesn't write messages to the error log even if "host" is missing. Default value is false

You can configure plugin in a production config file and define silent in a development config.

        # app.production.conf
        # redirect_host => {host => 'main.host'},

        # app.development.conf: 
        # redirect_host => {silent => 1},

CONFIG

You can pass options to the plugin with the help of your config. Use redirect_host key.

  $app->config(redirect_host => {host => 'main.host'});

TIP: use per mode config files (yourapp.production.conf) to pass parameters to the plugin to avoid redirection during development process

METHODS

register

Register. "register" in Mojolicious::Plugin

AUTHOR

alexbyk <alex@alexbyk.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by alexbyk.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.