++ed by:

1 PAUSE user

and 1 contributors


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


version 1.05


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 a Host header that is not equal to the host option will be redirected to the main host (and to the same port, as it was in original request) Don't forget about the port (don't expect something great from http://google.com:3000)

        http://www.main.host:3000       => http://main.host:3000
        http://another.io:3000/foo?bar  => http://main.host:3000/foo?bar

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)

Look at the `examples` directory of this distribution for a full application example



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


  $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


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},


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



Register. "register" in Mojolicious::Plugin


alexbyk <alex@alexbyk.com>


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.