目录
1. Create Your Custom 404 Page
2. Configure Apache Using .htaccess (Recommended for Shared Hosting)
3. Set It in Apache Virtual Host (Best for VPS or Dedicated Servers)
4. Test the Custom 404 Page
A Few Tips
首页 运维 Apache 如何在Apache中设置自定义404错误页面?

如何在Apache中设置自定义404错误页面?

Jul 27, 2025 am 02:20 AM
apache 404错误

要设置自定义404错误页面,需先创建页面文件并在Apache中配置路径。1. 创建自定义404页面,如将404.html保存至网站根目录;2. 在网站根目录的.htaccess文件中添加ErrorDocument 404 /404.html,确保AllowOverride已启用;3. 若使用VPS或独立服务器,可在虚拟主机配置文件(如000-default.conf)中添加ErrorDocument 404 /404.html,并重启Apache服务;4. 通过访问不存在的URL测试自定义页面是否生效。配置时应确保页面路径正确、HTTP状态码保持404,避免重定向至首页,以维护SEO和用户体验。

How to set a custom 404 error page in Apache?

Setting a custom 404 error page in Apache is straightforward and helps improve user experience when someone lands on a non-existent page. Here’s how to do it properly.

How to set a custom 404 error page in Apache?

1. Create Your Custom 404 Page

First, design a simple HTML page that will be shown when a 404 (Not Found) error occurs. For example:

<!-- 404.html -->
<!DOCTYPE html>
<html>
<head>
  <title>Page Not Found</title>
</head>
<body>
  <h1>404 - Page Not Found</h1>
  <p>The page you're looking for doesn't exist.</p>
  <p><a href="/">Go back to the homepage</a></p>
</body>
</html>

Save this file in your website’s root directory (e.g., /var/www/html/404.html) or any preferred location within your site structure.

How to set a custom 404 error page in Apache?

If you're using shared hosting or want to set this per-directory, use the .htaccess file in your site’s root:

ErrorDocument 404 /404.html

Make sure the path (/404.html) is correct relative to the web root. You can also point to a dynamic page:

How to set a custom 404 error page in Apache?
ErrorDocument 404 /error.php?code=404

? Ensure your .htaccess is readable by Apache and that AllowOverride is enabled in your Apache configuration for the directory.

3. Set It in Apache Virtual Host (Best for VPS or Dedicated Servers)

If you have access to the server configuration, it's better to define it in the virtual host block.

Edit your site’s config file (e.g., /etc/apache2/sites-available/000-default.conf or similar):

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html

    ErrorDocument 404 /404.html

    # Other directives...
</VirtualHost>

After making changes, restart Apache:

sudo systemctl restart apache2

or on some systems:

sudo service apache2 reload

4. Test the Custom 404 Page

To verify it works, navigate to a non-existent URL on your site:

http://yoursite.com/this-page-does-not-exist

You should see your custom 404 page instead of the default Apache error.

A Few Tips

  • The custom page should be a valid, accessible URL path.
  • Avoid redirecting 404s to the homepage — it’s bad for SEO and user trust.
  • Keep the HTTP status code as 404. Even with a custom page, browsers and search engines should receive the correct error code (Apache handles this automatically with ErrorDocument).
  • You can customize other errors too (like 500, 403) using the same method.

Basically, just create the page and tell Apache where to find it using ErrorDocument 404. Doesn’t matter much whether you use .htaccess or the server config — just pick the right method for your setup.

以上是如何在Apache中设置自定义404错误页面?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PhpStudy上部署Joomla网站的详细步骤 在PhpStudy上部署Joomla网站的详细步骤 May 16, 2025 pm 08:00 PM

在PhpStudy上部署Joomla网站的步骤包括:1)配置PhpStudy,确保Apache和MySQL服务运行并检查PHP版本兼容性;2)从Joomla官网下载并解压到PhpStudy的网站根目录,然后通过浏览器按照安装向导完成安装;3)进行基本配置,如设置网站名称和添加内容。

php写完代码怎么执行 php代码执行的几种常见方式 php写完代码怎么执行 php代码执行的几种常见方式 May 23, 2025 pm 08:33 PM

PHP代码可以通过多种方式执行:1.使用命令行,直接输入“php文件名”执行脚本;2.通过Web服务器,将文件放入文档根目录并通过浏览器访问;3.在IDE中运行,利用内置调试工具;4.使用在线PHP沙箱或代码执行平台进行测试。

Debian Tomcat更新如何操作 Debian Tomcat更新如何操作 May 28, 2025 pm 04:54 PM

在Debian系统中更新Tomcat版本一般包含以下流程:先行备份现有Tomcat版本执行更新操作前,务必先对现有的Tomcat环境做一个完整的备份工作。这涵盖了/opt/tomcat文件夹及其相关的配置文档,比如server.xml、context.xml和web.xml等。可以通过以下命令来完成备份任务:sudocp-r/opt/tomcat/opt/tomcat_backup获取新版本Tomcat前往ApacheTomcat的官方网站下载最新的版本。依据你的Debian系统

卸载Apache服务后系统性能未恢复的排查 卸载Apache服务后系统性能未恢复的排查 May 16, 2025 pm 10:09 PM

卸载Apache服务后系统性能未恢复的原因可能包括其他服务占用资源、日志文件中的错误信息、异常进程消耗资源、网络连接问题和文件系统残留。首先,检查是否有其他服务或进程在使用Apache卸载前的资源;其次,关注操作系统的日志文件,查找卸载过程中可能出现的错误信息;再者,检查系统的内存使用情况和CPU负载,找出异常进程;然后,使用netstat或ss命令查看网络连接情况,确保没有端口被其他服务占用;最后,清理卸载后残留的配置文件和日志文件,避免占用磁盘空间。

macOS系统启动Apache服务的命令及配置 macOS系统启动Apache服务的命令及配置 May 16, 2025 pm 10:00 PM

在macOS上启动Apache服务的命令是sudoapachectlstart,配置文件位于/etc/apache2/,主要步骤包括:1.编辑httpd.conf文件,修改Listen端口如Listen8080;2.调整DocumentRoot路径至个人目录如/Users/your_username/Sites,并更新相应的权限设置;3.使用sudoapachectlgraceful命令重启Apache以确保配置生效;4.启用mod_deflate模块压缩数据,提升页面加载速度。

解决卸载Apache过程中文件删除失败的问题 解决卸载Apache过程中文件删除失败的问题 May 16, 2025 pm 10:06 PM

Apache卸载过程中文件删除失败的原因包括文件权限问题、锁定文件和正在运行的进程。解决方法包括:1.停止Apache服务:sudosystemctlstopapache2;2.手动删除Apache目录:sudorm-rf/etc/apache2/usr/sbin/apache2;3.使用lsof查找并终止锁定文件的进程:sudolsof|grepapache2,然后sudokill-9;4.再次尝试删除文件。

Apache连接MySQL数据库的详细配置步骤 Apache连接MySQL数据库的详细配置步骤 May 16, 2025 pm 10:12 PM

配置Apache连接MySQL数据库需要以下步骤:1.确保已安装Apache和MySQL;2.配置Apache支持PHP,通过在httpd.conf或apache2.conf中添加LoadModule和AddHandler指令;3.配置PHP以连接MySQL,在php.ini中启用mysqli扩展;4.创建并测试连接的PHP文件。通过这些步骤,可以成功实现Apache与MySQL的连接。

Debian Hadoop监控工具有哪些 Debian Hadoop监控工具有哪些 May 23, 2025 pm 09:57 PM

在Debian系统上监控Hadoop集群有多种方法和工具,以下是一些常用的监控工具及其使用方法:Hadoop自带的监控工具HadoopAdminUI:通过浏览器访问HadoopAdminUI界面,直观了解集群状态及资源利用率。HadoopResourceManager:访问ResourceManagerWebUI(通常为http://ResourceManager-IP:8088),监控集群资源使用及作业状态。Hadoop

See all articles