如何使用ApachePerl处理程序重定向?

如何使用ApachePerl处理程序重定向?,apache,perl,redirect,cgi,cgi-bin,Apache,Perl,Redirect,Cgi,Cgi Bin,我有一个Apache处理程序,它将扩展名.redir设置为Perl脚本。代码如下: Action redir-url /cgi-bin/redir.pl AddHandler redir-url .redir 脚本只需将用户重定向到.redir文件中包含的页面。例如: so.redir: http://stackoverflow.com/ 如果用户访问http://example.com/so.redir,它们将被重定向到http://stackoverflow.com/ 我当前的代码如下所

我有一个Apache处理程序,它将扩展名
.redir
设置为Perl脚本。代码如下:

Action redir-url /cgi-bin/redir.pl
AddHandler redir-url .redir
脚本只需将用户重定向到
.redir
文件中包含的页面。例如:

so.redir

http://stackoverflow.com/
如果用户访问
http://example.com/so.redir
,它们将被重定向到
http://stackoverflow.com/

我当前的代码如下所示,尽管它返回一个错误500,并且可能完全关闭:

#!/usr/bin/perl
use strict;
use warnings;

use Path::Class;
use autodie;

my $file = file($ENV{'PATH_TRANSLATED'});

my $file_handle = $file->openw();

my @list = ('a', 'list', 'of', 'lines');

foreach my $line ( @list ) {
    # Add the line to the file
    $file_handle->print("Location: ".$line."\n\n");
}

谢谢你的帮助

在cgi时代,我们曾经有一个小的子程序来执行重定向:

sub redirect_url {
    my ($url, %params) = @_;

    $params{Location} = $url;

    if ( ($ENV{'HTTP_USER_AGENT'}=~m|Mozilla\/4\.|)
        && !($ENV{'HTTP_USER_AGENT'}=~m|MSIE|) ) {

        # handle redirects on netscape 4.x
        $params{Status} = '303 See Other'
            unless exists $params{Status};
        $params{'Content-Type'} = 'text/html; charset=utf-8'
            unless exists $params{'Content-Type'};
        $params{Content} =<<EOF;
<html>
  <head>
    <script language="JavaScript"><!--
location.href = "$params{Location}";
//--></script>
  </head>
  <body bgcolor="#FFFFFF">
    <a href="$params{Location}">Redirect</a>
  </body>
EOF
    }
    else {
            $params{Status} = '301 Moved Permanently'
            unless exists $params{Status};
        $params{'Content-Type'} = 'text/plain; charset=utf-8'
            unless exists $params{'Content-Type'};
    }

    $params{Expires} = 'Fri, 19 May 1996 00:00:00 GMT'
        unless exists $params{Expires};
    $params{Pragma} = 'no-cache'
        unless exists $params{Pragma};
    $params{'Cache-Control'} = 'no-cache'
        unless exists $params{'Cache-Control'};

    my $content = exists $params{Content}
        ? $params{Content} : $params{Status};
    delete $params{Content};

    while (my ($key, $value) = each %params) {
        print "$key: $value\n";
    }
    print "\n";
    print $content;

    exit 0;
}
子重定向\u url{
我的($url,%params)=@;
$params{Location}=$url;
if($ENV{'HTTP_USER_AGENT'}=~m|Mozilla\/4\.|)
&&!($ENV{'HTTP_USER_AGENT'}=~m|MSIE |){
#在netscape 4.x上处理重定向
$params{Status}='303请参见其他'
除非存在$params{Status};
$params{'Content-Type'}='text/html;charset=utf-8'
除非存在$params{'Content-Type'};

$params{Content}=标头缺少状态代码。您还在循环中输出标头,这毫无意义。您认为在一个响应中可以将客户端重定向到多少个位置?:)这只是从internet复制和粘贴不同的代码,因此有点不切实际:)您是否在开始时添加了she-bang行,以及有趣的地方Action定义?你设置了可执行位吗?没关系,工作得很好。我有一个错误的
use
语句,指向一个不存在的类。删除它,一切都已修复。
use strict;
my $file = $ENV{'PATH_TRANSLATED'};
open (my $fh, '<', $file) or die 'cant open';
my $url = <$fh>;
chomp($url);
redirect_url($url);