# translate.pl - mod_perl translation handler for Apache 2 to redirect # requests to the primary server name when it arrives on an alias name. # Also works correctly for virtual servers. # # Copyright 1998-2004 by Ian Kluft # Redistribution permitted by the author under the conditions of the # GNU General Public License Version 2. # http://www.kluft.com/~ikluft/opensource/GPLv2.txt # # To install: # 1) install the package in /etc/httpd/conf/perl/translate.pl # (if you install it elsewhere, adjust path in Step 2) # 2) In your httpd.conf or other appropriate config file, add # these lines or add them to an existing block: # # PerlRequire /etc/httpd/conf/perl/translate.pl # PerlTransHandler Trans::handler # package Trans; sub handler { my ( $r ) = @_; # check if the host name is provided and needs to be corrected my $req_host = $r->header_in("Host"); my $s = $r->server(); my $server_host = $s->server_hostname(); if (( defined $req_host ) and length($req_host) > 0 ) { my $port = $s->port(); if ( $req_host ne $server_host and $req_host ne "$server_host:$port" ) { my $url = "http://$server_host"; if ( $port != 80 and $port != 0 ) { $url .= ":$port"; } $url .= $r->uri(); $r->header_out("Location",$url); return &HTTP::Status::RC_MOVED_PERMANENTLY; } } return &Apache::DECLINED; } 1;