Home > Backend Development > PHP Tutorial > Detailed explanation of several methods to achieve page staticization in PHP

Detailed explanation of several methods to achieve page staticization in PHP

墨辰丷
Release: 2023-03-26 18:32:02
Original
2222 people have browsed it

这篇文章主要介绍了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;
Copy after login


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


echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($_SERVER);
preg_match(&#39;/^\/(\d+)\/(\d+)\.html/&#39;,$_SERVER[&#39;PATH_INFO&#39;],$arr);
print_r($arr);
Copy after login


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
Copy after login


如果服务器下不存在文件夹及其文件,那么就重写定义到/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;
Copy after login


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


相关推荐:

ThinkPHP 3.2.3实现页面静态化功能的方法

php中页面静态化实例详解

php页面静态化实例详解


The above is the detailed content of Detailed explanation of several methods to achieve page staticization in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template