站长资讯网
最全最丰富的资讯网站

php如何实现页面路由转发

php实现页面路由转发的方法:首先配置nginx服务器,在【.htaccess】中写上nginx的语法;然后打开根目录的【index.php】,编写文件路由即可。

php如何实现页面路由转发

php实现页面路由转发的方法:

1、配置nginx服务器

nginx服务器不会自动读取.htaccess,也不支持.htaccess语法,这里需要做一个投机取巧的方法:在.htaccess中写上nginx的语法,同时把该文件引入到nginx的配置中。这样就达到了和apache同样的目的。编辑.htaccess文件,输入以下内容并保存

if (!-e $request_filename){     rewrite ^(.*)$ /index.php; } location ~ /.ht {     deny  all; }

【解释】nginx匹配失败的uri全都转给index.php,同时禁止访问.htaccess文件

最重要的一步:在nginx配置中,在server{}内加入一句话:

include E:/demo/.htaccess;

【解释】将该文件原封不动的引入到nginx配置中。注意使用绝对路径!

2、编写index.php路由

打开根目录的index.php,输入以下内容

<?php     //路由     $uri = $_SERVER['REQUEST_URI']; //获取uri,例如 http://www.abc.com/study,其uri="/study"     switch($uri){         case "/":      include "template/home.php";  break;         case "/study": include "template/study.php"; break;         case "/play":  include "template/play.php";  break;     } 编写/template/下的网页文件 /template/下存放的网页文件,随便编辑点html用于测试。例如 home.php <!DOCTYPE html> <html> <head>     <meta charset="utf-8">     <title>这里是home</title> </head> <body>     <h1>你好,这里是home页面</h1> </body> </html>

效果

在浏览器访问http://localhost:8000 可以访问到/template/home.php

在浏览器访问http://localhost:8000/study 可以访问到/template/study.php

在浏览器访问http://localhost:8000/play 可以访问到/template/play.php

相关免费学习推荐:php编程(视频)

赞(0)
分享到: 更多 (0)