Home  >  Article  >  Web Front-end  >  Nginx实现404页面跳转到任意随机页面_html/css_WEB-ITnose

Nginx实现404页面跳转到任意随机页面_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:13:571324browse

一、概述

     很多时候我们访问一个网站,例如360,或者腾讯的页面,当该页面不存在的时候,会提示404未找到,然后右下角会出现 爱心帮助的 图片等,然后这些图片或者问题都是随机的,也许你下次访问的时候就是另外的页面了。现在我们通过nginx结合php来实现这么个需求。



二、配置

  前提: LEMP环境(略)


①编辑nginx配置文件(/etc/nginx/nginx.conf)

        location / {                    ###           if (!-e $request_filename) {                rewrite ^(.*)$  /error/404.php redirect;        }                    ####请求的时候判断请求的文件是否存在,不存在则显示重写到/error/404.php页面去            root   /webdoc/;            index  index.php index.html index.htm;        }


②编辑/error/404.php页面

<?php$num=mt_rand(1,9);   ##从1-9当中随机生成数字echo $num;echo "<img  src=$num.jpg alt="Nginx实现404页面跳转到任意随机页面_html/css_WEB-ITnose" >";   ##显示目录下的对应数字的图片?>


③添加本地图片


④测试

首页正常显示:

输入不存在的url时候效果:

回车后:



达到效果~~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn