The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

Version 0.03

SYNOPSIS

Generate 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 not equal to the host option will be redirected to the main host

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)

url

All keys of the url hash (except query) become Mojo::URL object's methods, regarding old request

  # 302: http://mirror.main.host/path?query -> http://main.host/path?query
  $app->plugin('RedirectHost', host => 'main.host', code => 302);

You can replace some parts of the old request, for example scheme (https), or add extra query parameters ?a=b to the end

  # http://mirror.main.host/foo -> https://main.host/foo?a=b
  $app->plugin(
    'RedirectHost',
    host   => 'main.host',    
    url => { scheme => 'https', query  => [{a => 'b'}] }
  );

How to use url->{query} option (pay attention to '[]')

  # append ?a=old&foo=bar -> ?a=old&foo=bar&a=b
  url => {query => [{a => 'b'}]

  # merge ?a=old&foo=bar -> ?a=b&foo=bar
  url => {query => [[a => 'b']]
  
  # replace ?a=old&foo=bar -> ?a=b
  url => {query => [a => 'b']}
  
  # this works too
  url => {query => [Mojo::Parameters->new(a => 'b')]}
  
  # Wrong!!! Don't do this. Don't forget []
  url => {query => Mojo::Parameters->new(a => 'b')}

See "query" in Mojo::URL

You can pass a string to the url part of options

  # http://mirror.main.host/foo -> http://google.com
  $app->plugin(
    'RedirectHost',
    host   => 'main.host',    
    url => 'http://google.com'
  );
  

New url as an Mojo::URL object

  # http://mirror.main.host/foo -> http://google.com
  $app->plugin(
    'RedirectHost',
    host => 'main.host',    
    url  => Mojo::URL->new('http://google.com')
  );

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'});

METHODS

register

Register. См "register" in Mojolicious::Plugin

TODO

Play around requests without "Host" header like this:

  GET / HTTP/1.1

AUTHOR

Alex, <alexbyk at cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-redirecthost at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-RedirectHost. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Mojolicious::Plugin::RedirectHost

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2012 Alex.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 178:

Non-ASCII character seen before =encoding in 'См'. Assuming CP1252