I use Mojolicious (not lite) and morbo server. How can I redirect request from http://www.domain.org at http://domain.org?
3 Answers
Your domain names www.domain.org and domain.org must be convert at the same ip address.
For example in your App.pm file you have this lines:
$r->get('/')->to('main#index')->name('index');
In your Mojolicious main controller you could check your domain name and do redirect:
sub index {
my $self = shift;
my $host = $self->req->url->to_abs->host;
return $self->redirect_to('http://domain.org') if $host eq 'www.domain.org';
}
Use morbo server only for debuging. For production use hypnotoad server.

- 891
- 6
- 15
You must own both domain names, and have either a CNAME or A record for www.domain.org point to domain.org. This is really DNS settings.
Now, in your Mojolicious app, you may need to account for the fact that you are receiving requests for www.domain.org/someroute from some users and requests for domain.org/someroute from others.
There is more than one way to to it - this is Perl, after all.
However, if you are deploying Mojolicious in production, I would recommend using a reverse proxy setup with Apache or Nginx.
This would allow you to receive requests from either www.domain.org or domain.org and proxy those requests to Hypnotoad. That way your app never has to worry about which hostname is being used to access it.
http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Nginx http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Apache%2Fmod_proxy

- 196
- 1
- 5