PHP 实现页面静态化的几种方法

所属分类: 网络编程 / PHP编程 阅读数: 802
收藏 0 赞 0 分享

1、通过buffer来实现

需要用file_put_contents ob_get_clean()等内置函数

ob_start ();
include "filterpost.html";
$mtime = filemtime("./filterpost.html");//在这里可以判断文件是否存在和过期,然后做缓存或者生成静态文件操作
$pageCache = str_replace('submit2','login',ob_get_contents());//将缓存去中的内容替换
ob_end_clean();
echo $mtime;
echo $pageCache;

2、通过$_SERVER['PATH_INFO']来实现

echo '<pre>';
print_r($_SERVER);
preg_match('/^\/(\d+)\/(\d+)\.html/',$_SERVER['PATH_INFO'],$arr);
print_r($arr);

3、通过Apache配置来实现

需要开启rewrite重写模块
通过rewrite来配置vhost

RewriteEngine on 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f 
RewriteRule ^/detail/([0-9]*).html$ /detail.php?id=$1 

如果服务器下不存在文件夹及其文件,那么就重写定义到/detail.php
http://localhost/detail/1.html
如果没有detail文件夹下的1.html 那么就重写定义到./detail.php

4、通过Nginx配置来实现

 在nginx.conf中配置 

rewrite ^/detail/(\d+)\.html$ /detail.php?id=$1 last;

当然建议大家参考一些比较成熟的cms的方法,对于页面数量不大的话,第一种方法还是不错的。

更多精彩内容其他人还在看

PHP Ajax实现页面无刷新发表评论

PHP Ajax实现页面无刷新发表评论
收藏 0 赞 0 分享

windows下PHP APACHE MYSQ完整配置

windows下PHP APACHE MYSQ完整配置
收藏 0 赞 0 分享

Ajax PHP分页演示

Ajax PHP分页演示
收藏 0 赞 0 分享

Smarty结合Ajax实现无刷新留言本实例

Smarty结合Ajax实现无刷新留言本实例
收藏 0 赞 0 分享

PHP 和 MySQL 开发的 8 个技巧

PHP 和 MySQL 开发的 8 个技巧
收藏 0 赞 0 分享

一个用php实现的获取URL信息的类

一个用php实现的获取URL信息的类
收藏 0 赞 0 分享

一个PHP操作Access类(PHP+ODBC+Access)

一个PHP操作Access类(PHP+ODBC+Access)
收藏 0 赞 0 分享

php你的验证码安全码?

php你的验证码安全码?
收藏 0 赞 0 分享

PHP无限分类的类

这段时间在写一个部门权限系统,需要用到无限分类技术,找了很多关于分类的代码.逐个收藏起来
收藏 0 赞 0 分享

phpwind中的数据库操作类

phpwind中的数据库操作类
收藏 0 赞 0 分享
查看更多