Apache访问错误404,重定向到友好页面

为了提升网站的用户体验,避免404、403之类的丑陋的默认错误提示出现,我们需要对错误页面进行优化,让他们显示的更加友好,错误页面不仅在于告诉用户访问出现了问题,而且需要引导用户到正确的页面

 

404丑陋的默认错误提示:

Apache访问错误404,重定向到友好页面

 

错误页面优雅化显示的实现方式主要有两种,下面我们主要以404错误为例:

 

1、修改Apache的配置文件;如下:

[[email protected] ~]# vim /etc/httpd/httpd.conf 

Apache读取的页面目录路径:/usr/local/apache/htdocs

Apache访问错误404,重定向到友好页面

添加如下内容:

Apache访问错误404,重定向到友好页面

 

2、在/usr/local/apache/htdocs/下编辑404.html文件内容,并且进行授权给apache用户;

[[email protected] ~]# cd /usr/local/apache/htdocs/
[[email protected] htdocs]# echo "welcome to zhanggeng Server;We Site change to www.zhanggeng.top" > 404.html
[[email protected] htdocs]# cat 404.html 
welcome to zhanggeng Server;We Site change to www.zhanggeng.top
[[email protected] htdocs]# chown -R apache:apache /usr/local/apache/htdocs/404.html     

正常情况下的网页:

[[email protected] htdocs]# cat index.html 
<html><body><h1>It works!</h1></body></html>

Apache访问错误404,重定向到友好页面

3、测试404友好页面是否生效;

[[email protected] htdocs]# /etc/init.d/apachectl graceful    #Apache平滑重启

访问192.168.169.205后面随便加目录,查看返回的是404默认界面,还是我们设置的界面;

Apache访问错误404,重定向到友好页面

返回的是我们之前写的内容;测试成功