How to make the page static in php

藏色散人
Release: 2023-04-06 19:52:02
Original
4834 people have browsed it

How to make the page static in php

页面静态化的好处

根据不同情况,有些需要生成静态页,有些实现伪静态即可,根据实际需求进行抉择。而静态化的好处,总结下来有以下几点:

● 提高访问速度

● 减少服务器压力

● 有利于SEO

● 提升网站稳定性

PHP生成静态页

PHP生成静态页有多种方法,此次使用的是ob系列函数,函数内容可查看官方手册。

<?php
$website = $_GET[&#39;website&#39;];
ob_start();// 打开输出缓存
$content = file_get_contents(&#39;http://www.&#39;.$website.&#39;.com&#39;);
echo $content;// 输出内容,此部分的内容为静态页的内容
file_put_contents(&#39;./&#39;.$website.&#39;.html&#39;, ob_get_contents());
ob_end_clean();// 关闭
echo $content;
Copy after login

Rewrite重写规则

在.htaccess中实现Rewrite重写规则,至于使用.htaccess是否影响效率不在讨论范围之内。

RewriteEngine on
# 如果有符合条件的静态页,返回静态页
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# 路由重写
RewriteRule ^(\w+).html$ /index.php?website=$1
Copy after login

测试

此时访问域名www.youWebsite.com/baidu.html,由于没有静态页,相当于请求到www.youWebsite.com/index.php?website=baidu。通过PHP的处理,已经生成baidu.html文件。

再次访问www.youWebsite.com/baidu.html,这时由于存在baidu.html文件,直接返回静态页,而不再请求PHP。

有几点注意事项:

● 检查apache是否开启Rewrite

● Linux下文件权限

The above is the detailed content of How to make the page static 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