动态页面静态化技术(很不错的教程摘录)

apache这样设置:
1、打开rewrite
#LoadModule rewrite_module "modules/mod_rewrite.so"
把#去掉。
2、#ADDModule mod_include.c
也把#去掉。
3、在适当地方放这些代码(请根据你的程序以及安装的位置作相应调整,如:我的论坛放在根目录的phpbb2,PA放在根目录的news)。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/phpbb2/viewforum([0-9a-zA-Z]+)-([0-9]+)-([0-9]+)\.html$ /phpbb2/viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^/phpbb2/forum([0-9a-zA-Z]+)\.html$ /phpbb2/viewforum.php?f=$1&mark=topic
RewriteRule ^/phpbb2/forum([0-9a-zA-Z]+)\.html$ /phpbb2/viewforum.php?f=$1
RewriteRule ^/phpbb2/prev-topic([0-9]+)\.html$ /phpbb2/viewtopic.php?t=$1&view=previous
RewriteRule ^/phpbb2/next-topic([0-9]+)\.html$ /phpbb2/viewtopic.php?t=$1&view=next
RewriteRule ^/phpbb2/viewtopic([0-9]+)-([0-9]+)\.html$ /phpbb2/viewtopic.php?t=$1&start=$2
RewriteRule ^/phpbb2/viewtopic([0-9]+)-([0-9]+)-([a-zA-Z]*)-([0-9]+)\.html$ /phpbb2/viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^/phpbb2/viewtopic([0-9]+)\.html$ /phpbb2/viewtopic.php?t=$1
RewriteRule ^/phpbb2/viewpost([0-9]+)\.html$ /phpbb2/viewtopic.php?p=$1
RewriteRule ^/phpbb2/viewtopic([0-9]+)\.html&highlight=(.*)$ /phpbb2/viewtopic.php?t=$1&highlight=$2
RewriteRule ^/phpbb2/profile([0-9]+)\.html$ /phpbb2/profile.php?mode=viewprofile&u=$1
RewriteRule ^/phpbb2/index.html$ /phpbb2/index.php
RewriteRule ^/phpbb2/faq.html$ /phpbb2/faq.php
RewriteRule ^/phpbb2/search.html$ /phpbb2/search.php
RewriteRule ^/phpbb2/memberlist.html$ /phpbb2/memberlist.php
RewriteRule ^/phpbb2/viewonline.html$ /phpbb2/viewonline.php
RewriteRule ^/phpbb2/modcp.html$ /phpbb2/modcp.php
RewriteRule ^/phpbb2/groupcp.html$ /phpbb2/groupcp.php
RewriteRule ^/phpbb2/login.html$ /phpbb2/login.php
RewriteRule ^/phpbb2/profile([0-9a-zA-Z]+)\.html$ /phpbb2/profile.php?mode=$1
RewriteRule ^/phpbb2/index.html$ /phpbb2/index.php?sid=$1
RewriteRule ^/phpbb2/c([0-9a-zA-Z]+)\.html$ /phpbb2/index.php?c=$1
RewriteRule ^/phpbb2/mark.html$ /phpbb2/index.php?mark=forums
RewriteRule ^/phpbb2/editprofile.html$ /phpbb2/profile.php?mode=editprofile
RewriteRule ^/phpbb2/inbox.html$ /phpbb2/privmsg.php?folder=inbox
RewriteRule ^/phpbb2/egosearch.html$ /phpbb2/search.php?search_id=egosearch
RewriteRule ^/phpbb2/unanswered.html$ /phpbb2/search.php?search_id=unanswered
RewriteRule ^/phpbb2/newposts.html$ /phpbb2/search.php?search_id=newposts
RewriteRule ^/phpbb2/register.html$ /phpbb2/profile.php?mode=register
RewriteRule ^/news/login.html$ /news/member.php?action=login
RewriteRule ^/news/logout.html$ /news/member.php?action=logout
RewriteRule ^/news/lastupdate.html$ /news/search.php?action=lastupdate
RewriteRule ^/news/hot.html$ /news/search.php?action=hot
RewriteRule ^/news/view.html$ /news/favorite.php?action=view
RewriteRule ^/news/modpassword.html$ /news/member.php?action=modpassword
RewriteRule ^/news/modprofile.html$ /news/member.php?action=modprofile
RewriteRule ^/news/forgetpassword.html$ /news/member.php?action=forgetpassword
RewriteRule ^/news/article/([0-9]).html$ /news/article.php?articleid=$1
RewriteRule ^/news/article/([0-9]+).([0-9a-zA-Z]+)\.html$ /news/article.php?articleid=$1&pagenum=$2
RewriteRule ^/news/sort/([0-9]+)\.html$ /news/sort.php?sortid=$1
RewriteRule ^/news/print/([0-9]+)\.html$ /news/print.php?articleid=$1
RewriteRule ^/news/recommend/([0-9]+)\.html$ /news/recommend.php?articleid=$1
RewriteRule ^/news/favorite/([0-9]+)\.html$ /news/favorite.php?articleid=$1&action=add
RewriteRule ^/news/comment/add/([0-9]+)\.html$ /news/comment.php?articleid=$1&action=add
RewriteRule ^/news/comment/view/([0-9]+)\.html$ /news/comment.php?articleid=$1&action=view
RewriteRule ^/news/pop.html$ /news/search.php?action=pop
RewriteRule ^/news/index.html$ /news/index.php
RewriteRule ^/news/usercp.html$ /news/usercp.php
RewriteRule ^/news/register.html$ /news/register.php
RewriteRule ^/news/search.html$ /news/search.php
RewriteRule ^/news/contribute.html$ /news/contribute.php
RewriteRule ^/news/register.html$ /news/register.php
RewriteRule ^/news/message/view/([0-9]+)\.html$ /news/message.php?action=view&commentid=$1
RewriteRule ^/news/message/reply/([0-9]+)\.html$ /news/message.php?action=reply&messageid=$1
RewriteRule ^/news/message/edit/([0-9]+)\.html$ /news/message.php?action=edit&messageid=$1
RewriteRule ^/news/message/view/([0-9]+).([0-9]+)\.html$ /news/message.php?action=view&commentid=$1&messageid=$2&messageid=&mode=flat
RewriteRule ^/news/comment/([0-9]+)\.html$ /news/comment.php?action=view&articleid=$1
RewriteRule ^/news/message/tree/([0-9]+)\.html$ /news/message.php?action=view&commentid=$1&messageid=&mode=tree
RewriteRule ^/news/message/flat/([0-9]+)\.html$ /news/message.php?action=view&commentid=$1&messageid=&mode=flat
RewriteRule ^/news/message/info/([0-9]+)\.html$ /news/message.php?action=view&commentid=$messageinfo[commentid]&messageid=$1
</IfModule>
4、对你的程序作相应的调整。
如原来是:$phparticleurl/article.php/$article[articleid]
改为:/$phparticleurl/article/$article[articleid].html
最后页面看起来应该是:
http://www.abc.com/article/123.html
其余类推。

asp动态页面静态化ISAPI_Rewrite是一个强大的基于正则表达式的URL处理引擎。它非常类似于Apache's mod_Rewrite,它是专为IIS设计的,经过过滤的url更符合友好url规则,更加容易被搜索引擎所接受,是一种SEO优化有力的方法。

当然前提你要自己有服务器(配置服务器iis的权限),
在此下载:http://www.isapirewrite.com/download/isapi_rwl_0055.msi
接着安装,随便装哪都行然后,打开Internet 信息服务,右键,web站点属性,点ISAPI筛选器选项卡.添加筛选器,名称填个isapi_rewrite,路径自己指定ISAPI_Rewrite.dll(在安装的根目录下),然后确定.

最后,
开始添加rewrite规则.正则,找到ISAPI_Rewrite目录,把httpd.ini的只读属性去掉,打开编辑.


下面附上几个通用的配置正则表达式,笔者试用通过:

例子:http://music.caiku.com http://flash.caiku.com

文件名、参数、参数值均在20位以内

一个参数通用表达式:/(.{1,20})/(\D{1,20})/(\d{1,20})\.html /$1\.asp\?$2=$3

两个参数通用表达式:/([^/]{1,20})/([^/]{1,20})/([^/]{1,20})/([^/]{1,20})/([^/]{1,20})\.html /$1\.asp\?$2=$3&$3=$4

三个参数通用表达式:/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})\.html /$1\.asp\?$2=$3&$3=$4&$5=$6

四个参数通用表达式:/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})\.html /$1\.asp\?$2=$3&$3=$4&$5=$6&$7=$8

五个参数通用表达式:/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})/([^/]{0,20})\.html /$1\.asp\?$2=$3&$3=$4&$5=$6&$7=$8&$9=$10

按我的思想就是以asp地址后所带的参数个数进行分类,这样比较通用,其它的当然可以举一反三,自己设计自己的需求

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度.
www.aspid.cn的主站就采用了TSYS生成html文件!
所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.

1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件 <%
filename="test.htm"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"
htmlwrite.write "<body>输出Title内容: " & request.form("title") & "<br /> 输出Body内容:" & request.form("body")& "</body></html>"
htmlwrite.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form" method="post" action="">
<input name="title" value="Title" size=26>
<br>
<textarea name="body">Body</textarea>
<br>
<br>
<input type="submit" name="Submit" value="生成html">
</form>
2,但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.
template.htm ' //模板文件 <html>
<head>
<title>$title$ by aspid.cn</title>
</head>
<body>
$body$
</body>
</html> ?

TestTemplate.asp '// 生成Html <%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 创建文件系统对象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'// 打开网页模板文件,读取模板内容
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))
strOut=f.ReadAll
htmlwrite.close

strTitle="生成的网页标题"
strContent="生成的网页内容"

'// 用真实内容替换模板中的标记
strOut=Replace(strOut,"$title$",strTitle)
strOut=Replace(strOut,"$body$",strContent)

'// 创建要生成的静态页
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)

'// 写入网页内容
htmlwrite.WriteLine strOut
htmlwrite.close

Response.Write "生成静态页成功!"

'// 释放文件系统对象
set htmlwrite=Nothing
set fso=Nothing
%>

3,第三种方法就是用XMLHTTP获取动态页生成的HTML内容,再用ADODB.Stream或者Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解.找到一段XMLHTTP生成Html的代码参考一下.
<%

'常用函数
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function

'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行转换
Function BytesToBstr(body,Cset)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function


txtURL=server.MapPath("../index.asp")

sText = getHTTPPage(txtURL)

Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename="../index.htm"
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing

%>
<script>
alert("静态网页生成完毕");
history.back();
</script>

1. 从http://www.isapirewrite.cn下载ISAPI_Rewrite Lite版,安装在硬盘上,如C:\Program Files\Helicon\ISAPI_Rewrite;
2. 打开IIS管理,选择要配置的主机,转到“ISAPI筛选器”选项卡。并添加一个筛选器,名称任意,执行文件为“C:\Program Files\Helicon\ISAPI_Rewrite\ISAPI_Rewrite.dll”;
3. 修改C:\Program Files\Helicon\ISAPI_Rewrite\httpd.ini文件,添加相应的过滤规则,例如:
我要将输入的网址localhost/54powerman显示localhost/blog.asp?id=54powerman,在配置文件中输入如下配置信息。
RewriteRule ^(/)([a-z0-9\-\_]+)$ /blog.asp?id=$2 [I,O]
4. 运行iisreset,重启IIS;这一步很重要,每次修改httpd.ini后都要重启以加载配置。
5. 测试,在站点首页目录添加新的blog.asp作为测试页面,输入代码:
<%
Response.Write "Hello," & Request.QueryString("id") & ".This is your blog."
%>
6. 在浏览器地址栏中输入http://localhost/54powerman,地址栏没有变化,但是网页中显示了如下内容:
Hello,54powerman.This is your blog.
7. 测试分析:http://localhost/54powerman这个url并不存在真实的文件,但是从用户看来好像存在真实的文件一样。
8. 试用后的思考:
8.1 如果RewriteRule ^(/soft_)([a-z0-9\-\_]+)(.html)$ /down.asp?softid=$2 [I,O],结果如何?
提示:输入http://localhost/soft_159746.html,结果为/down.asp?softid=159746的内容,这正好是一些下载站的方式。
8.2 如果RewriteRule ^(/)([a-z0-9\-\_]+)(/)([a-z0-9\-\_\.]+)$ /soft.asp?id1=$2&id2=$4 [I,O],结果如何?
提示:输入http://localhost/54powerman/photo.html,结果为id1=54powerman;id2=photo.html,这样可以让54powerman看起来像是一个虚拟目录
8.3 再复杂一些,如果RewriteRule ^(/)([a-z0-9\-\_\.\/]+)$ /soft.asp?id=$2 [I,O]呢?
提示:http://localhost/54powerman/myFolder/photos/list.asp,那么soft.asp中就会获得到id1=54powerman/myFolder/photos/list.asp,在soft.asp中来拆分各个参数,是不是就可以模拟更复杂的虚拟目录结构呢。
如下的soft.asp:
query = Request("id")
Response.Write "原始字符串:&nbsp;&nbsp;" & query &"<p>拆分后的字符串:<p>"
params = Split(query,"/")
For i=0 To UBound(params)
Response.Write 1+i & "级目录&nbsp;&nbsp;" & params(i) &"<p>"
Next
显示如下:
原始字符串: 54powerman/myFolder/photos/list.asp
拆分后的字符串:

1级目录 54powerman

2级目录 myFolder

3级目录 photos

4级目录 list.asp

9. 更多配置技巧,请参照C:\Program Files\Helicon\ISAPI_Rewrite\ISAPI_Rewrite.dll\documentation.htm

动态生成的网页在构造真正大型的网站时是不可缺少的。但是,动态页面的名字(即它的URL)往往缺乏明确的含义。例如,名为product.asp?Id=4的页面总不如名为/applicances/dishwashers/Model3809.htm的页面来得好记。本文介绍如何实现静态页面名字和动态页面的映射。
概述
静态页面名字除了具有含义更明确这一优点之外,它的另外一个优点在于搜索引擎能够索引这些页面。大多数搜索引擎,如Alta Vista和Yahoo,不会检索URL中带有问号的页面,因为它们担心进入永无止境的链接迷宫。把动态页面名字转换成静态页面名字之后,搜索引擎就会对这些页面进行索引和分类,从而为网站带来更多的访问量。
要使用本文介绍的方法将动态名字转换成静态名字,你必须使用Windows 2000和IIS 5.0。IIS 5.0的两个改进使得我们可以实现这种转换,它们是:使用Server.Transfer的“自定义错误信息”功能以及在ASP页面中使用Server.Transfer的能力。虽然IIS 4.0也支持自定义错误信息,但它使用的是Response.Redirect,没有用,所以本文方法要求使用IIS 5.0。Response.Redirect之所以没有用,是因为搜索引擎不会追随重定向。
使用本文所介绍的方法,网站开发者先用静态风格的URL链接事实上并不存在的页面。然后设置IIS服务器,告诉它要用指定的ASP页面(404.asp)来处理网站上出现的所有404错误。在这个404.asp页面里,把原来的URL转换成正式的动态URL,利用Server.Transfer执行并向用户浏览器返回目标页面。
假设有如下URL:
http://www.myserver.com/applicances/dishwashers/kenmore/Model3809.htm
其中myserver.com以网站的域名取代,该URL将返回404错误。我们首先要做的是用某个专用的.asp页面来处理所有这些404错误。这可以用IIS 5.0的“自定义错误信息”功能来实现。设置该功能的过程如下:

●在MMC中打开IIS服务器管理器
●右击Web网站节点,然后选择“属性”
●点击“自定义错误信息”页
●向下滚动,直至找到404错误
●双击404错误,打开“错误映射属性”对话框
●将消息类型改为URL
●在URL框输入“/404.asp”
●点击“确定”关闭窗口
现在404错误已经由404.asp处理了。当IIS调用404.asp页面时,它会提供导致这个404错误的URL,这对我们来说是非常有用的。
错误处理页面
接下来我们创建处理错误的404.asp页面。首先要做的是获得导致这个404错误的页面名字。下面这行代码从查询字符串提取这个页面的名字:
< %
'获得页面名字
strQ = Request.ServerVariables("QUERY_STRING")
% >
那么,strQ中的哪些内容才是重要的呢?在上例中它应该如下所示:/applicances/dishwashers/kenmore/Model3809.htm。我们真正需要的只有其中的Model3809(产品型号),因为这是产品数据库中的唯一键。下面这几行代码根据产品型号找出产品的ID(编号):
< %
' 计算产品型号
nIndex = InStrRev(strQ,"/")
If (nIndex >0) Then
strModelNumber = Right(strQ,Len(strQ)-nIndex)
strQ = Left(strQ,nIndex-1)
End If

' 从产品型号去掉.htm后缀
If ((Mid(strPageName,Len(strPageName),1)="m") And _
(Mid(strPageName,Len(strPageName)-1,1)="t") And _
(Mid(strPageName,Len(strPageName)-2,1)="h") And _
(Mid(strPageName,(strPageName)-3,1)=".")) Then
strModelNumber =Left(strPageName,Len(strModelNumber)-4)

End If

' 从产品数据库查找产品编号
strSQL = "SELECT Product_Id FROM Product " _
& "WHERE Product_Model = '" & strModelNumber & "'"
Set oRS = oConn.Execute(strSQL)
If (Not oRS.EOF) Then
lProductId = oRS("Product_Id")
End If
% >
现在我们得到了产品编号,在转换到正确的ASP页面之前必须先保存它。我们无法通过Server.Transfer在查询字符串中传递产品编号(这是IIS的一个局限),因此必须通过Session对象来传递它。
< %
Session("Id") = lProductId
Server.Transfer("/product.asp")
% >
执行这个页面时,浏览器地址栏中的内容如下:
http://www.myserver.com/applicances/dishwashers/kenmore/Model3809.htm
由于使用了Server.Transfer,浏览器地址栏中的URL不会改变,同样浏览器也不需要与服务器再交互一次,这与Response.Redirect是不同的。
另外也请注意URL中的目录是根本不存在的,事实上在这里URL中除了服务器名字之外的其余内容根本不起什么作用。例如,下面的几个URL都将返回同一页面。
http://www.myserver.com/Model3809.htm
http://www.myserver.com/trucks/ford/Model3809.htm
计算机教程用静态URL显示动态网页来自www.itwen.comIT WEN计算机教程网

那么为什么要加上这么多目录呢?这些目录将提高搜索引擎对网站的评价。一些搜索引擎认为URL中的单词是比页面标题和正文更重要的搜索关键词,所以目录名字是非常重要的。
调用不存在的页面
我们已经讨论了将并不存在的URL转换为有效动态URL的技术。然而,要让搜索引擎能够索引这些页面,我们还需要链接这些并不存在的URL。换句话说,只有当我们链接了Model3809.htm页面后搜索引擎才会发现它。
以前链接这种页面时我们只需按照如下方式给出产品编号:“product.asp?Id=4”。现在我们就来构造一个以产品编号为参数返回恰当URL的函数。
< %
Function CreateProductURL(lProductId)

strSQL = "SELECT Product_Model FROM Product " _ & "WHERE Product_Id =" & lProductId

Set oRS = oConn.Execute(strSQL)
If (Not oRS.EOF) Then
strModel = oRS("Product_Model")
End If
CreateProductURL = "/" & strModel & ".htm"

End Function
% >
现在如果我们要加入URL,只需按照如下方式进行:
< A HREF=/DownloadFiles\a200508\2005-08-07\"( %=CreateProductURL(4)% >" >洗碗机< /A >
本例我们假定不知道产品型号名称,必须查询数据库才能得知。但在现实中可能并非如此,我们知道产品的型号名称,可以直接创建URL而无需再次访问数据库。
性能问题
本文所介绍的方法会明显地影响性能。首先,为了创建静态风格的URL它需要一次数据库调用。其次,为了从产生404的URL出发得到正确的URL,它还要增加一次额外的数据库调用。最后,两次Server.Transfer需要较大的开销。
不过,有些性能问题可以用XCache(www.postpointsoft.com)来解决。XCache能够把出现404错误的URL转换到正确的页面,并缓存该信息以供后来的请求使用。

原文地址:https://www.cnblogs.com/liubaolongcool/p/1951827.html