【问题标题】:Apache ErrorDocument External Redirect with Variables带有变量的 Apache ErrorDocument 外部重定向
【发布时间】:2012-06-07 20:13:57
【问题描述】:

在 Apache 中是否可能发生这样的事情...(即重定向到外部 url,其路径导致传递 403 错误)

ErrorDocument 403 http://www.sample.com/{{REDIRECT_URL}}

【问题讨论】:

  • 其中 {{REDIRECT_URL}} 是请求的 url。

标签: apache errordocument


【解决方案1】:

ErrorDocument 配置选项,不幸的是,不支持服务器变量扩展。您可以尝试使用 local 脚本,该脚本将为您发出重定向。

ErrorDocument 403 /cgi-bin/error.cgi

请注意,该脚本必须是本地的,否则您将无法通过 REDIRECT_* 变量。在脚本本身中,您可以发出重定向语句:

#!/usr/bin/perl
my $redirect_status = $ENV{'REDIRECT_STATUS'} || '';
my $redirect_url = $ENV{'REDIRECT_URL'} || '';

if($redirect_status == 403) {
    printf("Location: http://%s%s\n", 'www.sample.com', $redirect_url);
    printf("Status: %d\n", 302);
}
print "\n";

有关更多见解,请参阅 Apache 文档http://httpd.apache.org/docs/2.4/custom-error.html

【讨论】:

    【解决方案2】:

    我创建 /Publication 目录,但我希望这将由主 index.php 提供服务,因为该目录中有文件。

    /Publication?page=1 将由 /index.php 提供,/Publication/file.pdf 是驻留在此目录中的文件。

    Apache 返回 403 错误,因为 /Publication 目录无权列出。当您将 403 错误重定向到 /index.php 时,您无法捕获变量。我认为可以使用 REDIRECT_QUERY_STRING 变量,但这会弄乱我的 php 类。

    我更改了 mod_rewrite 配置以捕获目录服务,请参阅“#”,删除 ErrorDocument 403 指令,因为不需要。

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
    #  RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
    </IfModule>
    

    我有什么

    /Publication > /index.php?path=Publication
    
    /Publication/?page=1  > /index.php?path=Publication&page=1
    
    /Publication/file.pdf > /Publication/file.pdf
    

    【讨论】:

    • 你的.htaccess在哪里?它在/Publication 目录内吗?如果是这样,您的.htaccess 中是否有DirectorySlash Off?否则,如果设置了DirectorySlash On,你怎么能说/Publication &gt; /index.php?path=Publication默认Apache会将/Publication重定向到/Publication/,然后重写规则将重写/Publication/ &gt; /index.php?path=Publication/。我问你只是因为我对你的设置感到好奇,我想直接将 /Publication 映射到 /index.php?path=Publication 而不让 Apache 重定向到 /Publication/
    【解决方案3】:

    是的!但仅从 Apache 2.4.13 开始:

    从 2.4.13 开始,可以在指令内部使用表达式语法来 生成动态字符串和 URL。

    (来自https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

    以下配置将导致 HTTP 302 响应:

    ErrorDocument 403 http://www.example.com%{REQUEST_URI}
    

    请注意缺少斜线,因为%{REQUEST_URI} 以斜线开头。

    【讨论】:

      【解决方案4】:

      我认为这是可能的,因为文档说 ..http://httpd.apache.org/docs/2.0/mod/core.html#errordocument

      也许是这样的:

      ErrorDocument 403 http://www.sample.com/?do=somthing
      

      【讨论】:

      • 那没有添加请求的网址......我以为我的问题很清楚。
      猜你喜欢
      • 2011-08-11
      • 2012-10-28
      • 1970-01-01
      • 2017-10-07
      • 2016-07-05
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多