编辑 httpd.conf 文件以在 linux 中自定义 404 错误

Posted

技术标签:

【中文标题】编辑 httpd.conf 文件以在 linux 中自定义 404 错误【英文标题】:Edit httpd.conf file for custom 404 error in linux 【发布时间】:2013-09-21 20:22:05 【问题描述】:

我创建了一个自定义的 404 错误页面并将其保存到此位置;

/var/www/error/error_404.php

我需要在 httpd.conf 文件中进行哪些更改,以使此错误显示在我网站上不正确的网页上?到目前为止,我已尝试将以下行添加到文件中的多个位置;

ErrorDocument 404 /error/error_404.php

然后错误页面会显示类似的内容;

未找到

在此服务器上未找到请求的 URL /indfdex.php/

此外,在尝试使用 ErrorDocument 时遇到 403 Forbidden 错误 处理请求。

我的 httpd.conf 文件的一部分如下所示;

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#

#
# Putting this all together, we can internationalize error responses.
#
# We use Alias to redirect any /error/HTTP_<error>.html.var response to
# our collection of by-error message multi-language collections.  We use 
# includes to substitute the appropriate text.
#
# You can modify the messages' appearance without changing any of the
# default HTTP_<error>.html.var files by adding the line:
#
#   Alias /error/include/ "/your/include/path/"
#
# which allows you to create your own set of files by starting with the
# /var/www/error/include/ files and
# copying them to /your/include/path/, even on a per-VirtualHost basis.
#

Alias /error/ "/var/www/error/"

<IfModule mod_negotiation.c>
<IfModule mod_include.c>
    <Directory "/var/www/error">
        AllowOverride None
        Options IncludesNoExec
        AddOutputFilter Includes html
        AddHandler type-map var
        Order allow,deny
        Allow from all
        LanguagePriority en es de fr
        ForceLanguagePriority Prefer Fallback

    </Directory>

#    ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
#    ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
#    ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
#    ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
#    ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
#    ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
#    ErrorDocument 410 /error/HTTP_GONE.html.var
#    ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
#    ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
#    ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
#    ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
#    ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
#    ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
#    ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
#    ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
#    ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
#    ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var

</IfModule>
</IfModule>

【问题讨论】:

【参考方案1】:

你必须把你的 404 error.php 放在 apache 的 DocumentRoot 目录下,然后测试它。

感谢和问候, 阿洛克·萨克

【讨论】:

我可以确认这是可行的。我将 ErrorDocuement 404 行放在 mod_include.c 函数中

以上是关于编辑 httpd.conf 文件以在 linux 中自定义 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在Linux 中搭建一个web服务器

linux如何设置软连接??

Linux 安装SSL证书

Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)

Linux中apache服务

终端.app打开Apache的命令™s码httpd.conf文件文本编辑中的文件