网站页面静态化有什么好处?怎么实现网页静态化

html-css09

网站页面静态化有什么好处?怎么实现网页静态化,第1张

静态网页是服务器上面真实存在的页面,它不需要编译,用户就可以直接访问静态网页。

在网站优化工作中,静态化页面对SEO非常友好:

1、网页打开速度快,因为是直接读取文件;

2、有利于搜索引擎的抓取收录;

3、静态网页相对比较稳定,对服务器友好;

4、一些面对数据库的攻击比如SQL注入攻击,在面对静态网页的时候常常难以从地址入手。

动态页面如aspx,php等以html,shtml等形式表示

可用模板或urlrewriter静态化

public static bool WriteFile(string strText,string strContent,string strAuthor)

{

string path = HttpContext.Current.Server.MapPath("/news/")

Encoding code = Encoding.GetEncoding("gb2312")

string temp = HttpContext.Current.Server.MapPath("/news/template.html")

StreamReader sr=null

StreamWriter sw=null

string str=""

try

{

sr = new StreamReader(temp, code)

str = sr.ReadToEnd()

}

catch(Exception exp)

{

HttpContext.Current.Response.Write(exp.Message)

HttpContext.Current.Response.End()

sr.Close()

}

public static string getUrltoHtml(string Url)

{

errorMsg = ""

try

{

System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url)

System.Net.WebResponse wResp =wReq.GetResponse()

System.IO.Stream respStream = wResp.GetResponseStream()

System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"))

return reader.ReadToEnd()

}

catch(System.Exception ex)

{

errorMsg = ex.Message

}

return ""

}

一、静态化的优点:

1有利于搜索引擎收录网站页面的信息:搜索引擎更喜欢静态的,更变于抓取,搜索引擎SEO排名会更容易提高。

2静态网页化网页稳定

3可以提高网页加载速度

4减轻服务器负担,浏览器不需要频发调用数据库。

5数据库出错不会影响正常访问

二、伪静态

PHP伪静态:是利用Apache mod_rewite实现url重写的方法

改写访问地址,能够通过URL的PATHINFO模式来改动它。让它看上去更像一个静态页面。从而有更大的几率被搜索引擎抓取和收录,仅是对搜索引擎比较友好,伪静态化

三、纯静态化

纯静态化,就是生成HTML文件的方式,我们须要开启PHP自带的缓存机制,即ob_start来开启缓存。而且在ob_start之前不能有不论什么输出,否则运行失败,然后我们用ob_get_contents函数来获取缓存中的内容,该函数会返回一个字符串。第三个函数就是ob_end_clean,它用来清空缓存中的内容而且关闭,成功返回True,失败返回False。

<?php

if(file_exists("match.html") &&(time()-filemtime("match.html"))<300)

{

//如果存在对应的静态文件,则直接访问

require_once("match.html")

}else{

//这里用数组来仿真数据库操作,现实一般都是操作数据库得到对应数据

$student = array("huangxing","chenyuwei",'xufei','zhangjingwen','xuepei')

ob_start()

//载入模板

require_once("muban.php")

file_put_contents("match.html",ob_get_clean())

require_once('match.html')

}