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

yipeiwu_com6年前PHP代码库

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的方法,对于页面数量不大的话,第一种方法还是不错的。

相关文章

深入array multisort排序原理的详解

复制代码 代码如下:<?php$data[] = array('volume' => 67, 'edition' => 2);$data[] = array('volu...

PHP初学者最感迷茫的问题小结

【1】页面之间无法传递变量 get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_POST['f...

php中判断文件存在是用file_exists还是is_file的整理

看了这篇PHP中file_exists与is_file,is_dir的区别的说法基本明白,PHP的 file_exists = is_dir + is_file。 写程序验证一下: 分别...

PHP中error_reporting()用法详解

error_reporting() 函数规定报告哪个错误 。该函数设置当前脚本的错误报告级别。该函数返回旧的错误报告级别。 首先要知道error_reporting()函数是用来设置错误...

适用于php-5.2 的 php.ini 中文版[金步国翻译]

由于作者水平有限,因此不能保证作品内容准确无误,请在阅读中自行鉴别。如果你发现了作品中的错误,请您来信指出,哪怕是错别字也好,任何提高作品质量的建议我都将虚心接纳。如果你愿意就作品中的相...