Home > php教程 > php手册 > body text

Z-Blog分类标签全站静态化方案

WBOY
Release: 2016-06-21 08:51:36
Original
1156 people have browsed it

Z-Blog目前已经实现了首页和文章页的静态化,可以生成HTML文件,但是系统的分类页、标签Tags页、归档页和作者页都没有实现静态化,本文将讲述一个非常简单的方案,能够自动生成各个分类页、归档页和Tags的静态HTML页面,以便用户将网站迁移到Apache等不支持ASP的主机上。

  Z-Blog的分类页、标签Tags页、归档页和作者页都是调用catalog.asp这个文件,因此,修改这个文件,当用户调用该文件的时候,自动生成针对该页面的HTML文件。如果访客反复读取这个文件,那么在一定时间范围内,可以直接在文件开头使用静态文件,以达到节省系统资源的目的。

  具体的修改方法是,先在根下建立一个目录cat,然后使用编辑器打开Z-Blog根目录下的catalog.asp文件,在文件开头加入如下的代码:

Dim objFSO
Dim objFile
Dim strFileName
Dim strFileTime
Dim isBuildFile
if Request.QueryString("cate")"" then
 if Request.QueryString("page")""then
  strFileName = "cate" + "_" + Request.QueryString("cate") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "cate" + "_" + Request.QueryString("cate") + ".html"
 end if
elseif Request.QueryString("tags")"" then
 if Request.QueryString("page")""then
  strFileName = "tags" + "_" + Request.QueryString("tags") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "tags" + "_" + Request.QueryString("tags") + ".html"
 end if
elseif Request.QueryString("auth")"" then
 if Request.QueryString("page")""then
  strFileName = "auth" + "_" + Request.QueryString("auth") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "auth" + "_" + Request.QueryString("auth") + ".html"
 end if
elseif Request.QueryString("date")"" then
 if Request.QueryString("page")""then
  strFileName = "date" + "_" + Request.QueryString("date") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "date" + "_" + Request.QueryString("date") + ".html"
 end if
elseif Request.QueryString("page")"" then
 strFileName = "default" + "_" + Request.QueryString("page") +".html"
else
 strFileName = "default_1" + ".html"
end If
isBuildFile = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Server.MapPath(strFileName)) Then
 Set objFile = objFSO.GetFile(Server.MapPath(strFileName))
 strFileTime = objFile.DateLastModified
 Set objFile = Nothing
 If datediff("h",strFileTime,Now()) > 1 Then
  isBuildFile = True
 Else
  Server.Transfer strFileName
  Response.End
 End If
Else
 isBuildFile = True
End If
Set objFSO = Nothing

  找到 Response.Write ArtList.html 一行,在其后面增加如下代码:

If isBuildFile Then
    ArtList.FileName=strFileName
    ArtList.Directory="cat"
    ArtList.Save
End if

  这样,系统就会自动生成全部动态页面的静态HTML文件,并且在1小时内不会重复生成,期间如果还有调用,则自动载入静态HTML文件,以节省系统资源。

  经过这番处理,Z-Blog的全部分类和Tags就都可以生成静态HTML页面了。

  经过静态化之后,就可以将Z-Blog全站都放在PHP+Apache的主机上了。具体方法是,配置Apache主机,使其用php的方法解析asp,然后编辑catalog.asp这个文件,将其代码修改为如下即可:

if (isset($_GET['cate'])) {
 if (isset($_GET['page'])) {
  $strFileName = "cate_".strip_tags(stripslashes(trim($_GET['cate'])))."_".strip_tags(stripslashes(trim($_GET['page']))).".html";
 } else {
  $strFileName = "cate_".strip_tags(stripslashes(trim($_GET['cate']))).".html";
 }
} else if (isset($_GET['tags'])) {
 if (isset($_GET['page'])) {
  $strFileName = "tags_".strip_tags(stripslashes(trim($_GET['tags'])))."_".strip_tags(stripslashes(trim($_GET['page']))).".html";
 } else {
  $strFileName = "tags_".strip_tags(stripslashes(trim($_GET['tags']))).".html";
 }
} else if (isset($_GET['auth'])) {
 if (isset($_GET['page'])) {
  $strFileName = "auth_".strip_tags(stripslashes(trim($_GET['auth'])))."_".strip_tags(stripslashes(trim($_GET['page']))).".html";
 } else {
  $strFileName = "auth_".strip_tags(stripslashes(trim($_GET['auth']))).".html";
 }
} else if (isset($_GET['date'])) {
 if (isset($_GET['page'])) {
  $strFileName = "date_".strip_tags(stripslashes(trim($_GET['date'])))."_".strip_tags(stripslashes(trim($_GET['page']))).".html";
 } else {
  $strFileName = "date_".strip_tags(stripslashes(trim($_GET['date']))).".html";
 }
} else if (isset($_GET['page'])) {
 $strFileName = "default_".strip_tags(stripslashes(trim($_GET['page']))).".html";
} else {
 $strFileName = "../default.html";
}
define("HTML_FILE",  dirname(__FILE__)."/".$strFileName);
if (file_exists(HTML_FILE)) {
 echo(file_get_contents(HTML_FILE));
 exit(0);
} else {
 exit(0);
}
 



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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!