⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custom-error.html

📁 这个是我在web培训时老师提供的手册
💻 HTML
字号:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="Apache, 中文, 手册, 中文版, 中文手册, 中文版手册, 参考手册, 中文参考手册, 金步国" />
<meta name="description" content="Apache 2.2 中文版参考手册" />
<meta name="author" content="金步国" />
<link href="./style/css/manual-zip.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="./style/css/manual-zip-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<link href="./style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<title>自定义错误响应 - Apache 2.2 中文版参考手册</title>
</head>
<body id="manual-page">
<div id="page-header"><p class="menu"><a href="./mod/index.html">模块索引</a> | <a href="./mod/directives.html">指令索引</a> | <a href="./faq/index.html">常见问题</a> | <a href="./glossary.html">词汇表</a> | <a href="./sitemap.html">站点导航</a></p><p class="apache">Apache HTTP Server 版本2.2</p><img alt="" src="./images/feather.gif" /></div>
<div class="up"><a href="./index.html"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div>
<div id="path"><a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">文档</a> &gt; <a href="./index.html">版本2.2</a></div>

<div id="translation-info">   <a href="./translator_announcement.html#thanks">致谢</a> | <a href="./translator_announcement.html#announcement">译者声明</a> | 本篇译者:<a href="mailto:&#099;sfr&#0097;nk&#0064;&#099;itiz&#046;n&#0101;t">金步国</a> | 本篇译稿最后更新:2006年1月2日 | <a href="./translator_announcement.html#last_new">获取最新版本</a></div>
<div id="page-content"><div id="preamble"><h1>自定义错误响应</h1>


    <p>Apache可以让网站管理员自己自定义对一些错误和问题的响应。</p>

    <p>自定义的响应可以定义为当服务器检测到错误或问题时才被激活。</p>

    <p>如果一个脚本崩溃并产生"500 Server Error"响应,那么这个响应可以被更友好的提示替换或者干脆用重定向语句跳到其他的URL(本地的或外部的)。</p>
  </div>
	<div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
<div class="section">
<h2><a name="behavior" id="behavior">行为</a></h2>
    

    <h3>老式的行为</h3>
      

      <p>Apache1.3 会响应一些对于用户没有任何意义的错误或问题信息,而且不会将产生这些错误的原因写入日志。</p>
    

    <h3>新式的行为</h3>
      

      <p>服务器可以被要求作出如下应答:</p>

      <ol>
        <li>显示一些其他的文字以代替硬编码的信息</li>

        <li>重定向到本地URL</li>

        <li>重定向到一个外部的URL</li>
      </ol>

      <p>当一些信息可以被传递的时候,重定向到另外一个URL就变得很有用。这些信息用于更清楚的解释和/或记录一些错误或问题产生的原因。</p>

      <p>为了达到这个目的,Apache将定义一些新的类似于CGI环境变量的环境变量:</p>

      <div class="example"><p><code>
        REDIRECT_HTTP_ACCEPT=*/*, image/gif, image/x-xbitmap, image/jpeg<br />
        REDIRECT_HTTP_USER_AGENT=Mozilla/1.1b2 (X11; I; HP-UX A.09.05 9000/712)<br />
        REDIRECT_PATH=.:/bin:/usr/local/bin:/etc<br />
        REDIRECT_QUERY_STRING=<br />
        REDIRECT_REMOTE_ADDR=121.345.78.123<br />
        REDIRECT_REMOTE_HOST=ooh.ahhh.com<br />
        REDIRECT_SERVER_NAME=crash.bang.edu<br />
        REDIRECT_SERVER_PORT=80<br />
        REDIRECT_SERVER_SOFTWARE=Apache/0.8.15<br />
        REDIRECT_URL=/cgi-bin/buggy.pl
      </code></p></div>

      <p>请注意"<code>REDIRECT_</code>"这个前缀。</p>

      <p>至少会有<code>REDIRECT_URL</code>和<code>REDIRECT_QUERY_STRING</code>两个变量会被传递到新的URL(假定这个URL是cgi脚本或者是cgi包含页面)。其他变量将仅在发生错误或问题之前存在的情况下才存在。如果你的<code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code>使用了<em>外部</em>重定向(任何类似于<code>http:</code> 开头的形式,哪怕它仍指向同一个服务器),将<strong>没有</strong>任何变量被指定。</p>
    
  </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
<div class="section">
<h2><a name="configuration" id="configuration">配置</a></h2>
    

    <p>当对<code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code>进行了相应的设置后,将可以在<code>.htaccess</code>文件中使用<code class="directive"><a href="./mod/core.html#allowoverride">AllowOverride</a></code>指令。</p>

    <p>以下是一些示例...</p>

    <div class="example"><p><code>
      ErrorDocument 500 /cgi-bin/crash-recover <br />
      ErrorDocument 500 "Sorry, our script crashed. Oh dear" <br />
      ErrorDocument 500 http://xxx/ <br />
      ErrorDocument 404 /Lame_excuses/not_found.html <br />
      ErrorDocument 401 /Subscription/how_to_subscribe.html
    </code></p></div>

    <p>语法如下:</p>

    <div class="example">
    	<p><code>
      ErrorDocument &lt;3位错误代码&gt; &lt;action&gt;
    </code></p>
    </div>

    <p>&lt;action&gt;可以代表:</p>

    <ol>
      <li>用于显示的用双引号(")界定的文字。双引号之间的所有文字都将被显示,<em>但双引号本身不会被显示</em></li>

      <li>作为重定向目的外部URL</li>

      <li>作为重定向目的本地URL</li>
    </ol>
  </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
<div class="section">
<h2><a name="custom" id="custom">自定义错误响应与重定向</a></h2>
    

    <p>Apache重定向到URL的行为已经进行了修改,以便可以在脚本/服务器端包含页面加入额外的环境变量。</p>

    <h3>老式的行为</h3>
      

      <p>标准CGI变量对于重定向的目的脚本来说是可见的。但没有说明重定向的来源。</p>
    

    <h3>新式的行为</h3>
      

      <p>一批新的环境变量将被初始化并提供给重定向的目标脚本。每个新变量都有一个"<code>REDIRECT_</code>"前缀。<code>REDIRECT_*</code> 环境变量由重定向之前的CGI环境变量创建而来,并被加上了"<code>REDIRECT_</code>"前缀。比如说,<code>HTTP_USER_AGENT</code>变成了<code>REDIRECT_HTTP_USER_AGENT</code> 。在这些新变量之外,Apache还将定义<code>REDIRECT_URL</code>和<code>REDIRECT_STATUS</code>来帮助脚本确定重定向的来源。重定向的源URL和目的URL都能被记录到访问日志中。</p>

      <p>如果<code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code>指定了一个到本地CGI脚本的重定向,该脚本应当在它的输出中包含一个"<code>Status:</code>"头字段以确保将导致调用它的错误条件始终返回客户端。举例来说,一个Perl <code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code>脚本可能包含如下内容:</p>

      <div class="example"><p><code>
        ... <br />
        print  "Content-type: text/html\n"; <br />
        printf "Status: %s &lt;中断条件&gt;\n", $ENV{"REDIRECT_STATUS"}; <br />
        ...
      </code></p></div>

      <p>如果该脚本专门用于处理一个特定的错误条件,比如:<code>404 Not Found</code> ,它就可以使用特定的代码和错误文本进行替代。</p>

      <p>需要注意的是如果应答包含一个"<code>Location:</code>"头(为了进行一个客户端重定向),脚本<em>必须</em>发出一个适当的"<code>Status:</code>"头(比如:<code>302 Found</code>)。否则"<code>Location:</code>"头可能无效。</p>
    
  </div></div>
<div id="footer">
<p class="apache">本文允许自由使用、分发、转载,但必须保留译者署名;详见:<a href="./translator_announcement.html#announcement">译者声明</a>。</p>
<p class="menu"><a href="./mod/index.html">模块索引</a> | <a href="./mod/directives.html">指令索引</a> | <a href="./faq/index.html">常见问题</a> | <a href="./glossary.html">词汇表</a> | <a href="./sitemap.html">站点导航</a></p></div>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -