PHP China | 中国开源之路 's Archiver

lx4909 发表于 2008-11-16 14:08

通过URL重写实现伪静态

[b][font=宋体][size=16pt]一.[/size][/font][/b][b][size=16pt][font=Times New Roman]Apache[/font][/size][/b][b][font=宋体][size=16pt]设置[/size][/font][/b][b]
[/b]
[b][font=宋体]独立主机用户[/font]
[/b]
[font=Times New Roman]Apache [/font][font=宋体]基本配置:[/font]
[font=宋体][size=10.5pt]首先确定您使用的[/size][/font][font=Arial][size=10.5pt] Apache [/size][/font][font=宋体][size=10.5pt]版本,及是否加载了[/size][/font][font=Arial][size=10.5pt] mod_Rewrite [/size][/font][font=宋体][size=10.5pt]模块。[/size][/font]

[font=Times New Roman][/font]
[font=Arial][size=10.5pt]Apache 1.x [/size][/font][font=宋体][size=10.5pt]的用户请检查[/size][/font][font=Arial][size=10.5pt] conf/httpd.conf [/size][/font][font=宋体][size=10.5pt]中是否存在如下两段代码:[/size][/font]

[font=Times New Roman]LoadModule Rewrite_module libexec/mod_Rewrite.so[/font]
[font=Times New Roman]AddModule mod_Rewrite.c[/font]
[font=Times New Roman][/font]
[font=Arial][size=10.5pt]Apache 2.x [/size][/font][font=宋体][size=10.5pt]的用户请检查[/size][/font][font=Arial][size=10.5pt] conf/httpd.conf [/size][/font][font=宋体][size=10.5pt]中是否存在如下一段代码:[/size][/font]
[font=Arial][size=10.5pt]LoadModule Rewrite_module modules/mod_Rewrite.so[/size][/font]


[font=宋体][size=10.5pt]如果没有安装[/size][/font][font=Arial][size=10.5pt] mod_Rewrite[/size][/font][font=宋体][size=10.5pt],您可以重新编译[/size][/font][font=Arial][size=10.5pt] Apache[/size][/font][font=宋体][size=10.5pt],并在原有[/size][/font][font=Arial][size=10.5pt] configure [/size][/font][font=宋体][size=10.5pt]的内容中加入[/size][/font][font=Arial][size=10.5pt] --enable-Rewrite=shared[/size][/font]
[font=Times New Roman][/font]
[font=宋体][size=10.5pt]注:如果前面有[/size][/font][font=Arial][size=10.5pt]#[/size][/font][font=宋体][size=10.5pt],将其去掉。[/size][/font]



[font=宋体]方法一:通过配置[/font][font=Times New Roman]Apache[/font][font=宋体]配置文件[/font][font=Times New Roman]httpd.conf[/font][font=宋体]实现[/font][font=Times New Roman]URL[/font][font=宋体]重写[/font]
[font=宋体][size=10.5pt]在配置文件(通常就是[/size][/font][font=Arial][size=10.5pt] conf/httpd.conf[/size][/font][font=宋体][size=10.5pt])中加入如下代码。[/size][/font]
[font=Times New Roman]<IfModule mod_Rewrite.c>[/font]
[font=Times New Roman]RewriteEngine On[/font]
[font=Times New Roman]RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2[/font]
[font=Times New Roman]RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2[/font]
[font=Times New Roman]</IfModule>[/font]
[font=宋体]注:[/font][font=宋体][size=10.5pt]此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即[/size][/font][font=Arial][size=10.5pt] <VirtualHost> [/size][/font][font=宋体][size=10.5pt]中去,如果加在虚拟主机配置外部将可能无法使用,改好后将[/size][/font][font=Arial][size=10.5pt] Apache [/size][/font][font=宋体][size=10.5pt]重启。[/size][/font]



[font=宋体]方法二:通过在根目录中的跨越配置文件[/font][font=Times New Roman].htaccess[/font][font=宋体]实现[/font][font=Times New Roman]URL[/font][font=宋体]重写[/font]
[size=10.5pt][font=Times New Roman]1.
[/font][/size][font=宋体][size=10.5pt]配置[/size][/font][font=Arial][size=10.5pt]apache[/size][/font][font=宋体][size=10.5pt]支持对[/size][/font][font=Arial][size=10.5pt] .htaccess [/size][/font][font=宋体][size=10.5pt]文件的解析[/size][/font]

[font=宋体][size=10.5pt]查找:[/size][/font]

[font=Arial][size=10.5pt]<Directory />
[/size][/font]
[font=Arial][size=10.5pt]
Options FollowSymLinks
[/size][/font]
[font=Arial][size=10.5pt]
AllowOverride None
[/size][/font]
[font=Arial][size=10.5pt]</Directory> [/size][/font]


[font=宋体][size=10.5pt]修改为:[/size][/font]

[font=Arial][size=10.5pt]<Directory />
[/size][/font]
[font=Arial][size=10.5pt]
Options FollowSymLinks
[/size][/font]
[font=Arial][size=10.5pt]
AllowOverride All
[/size][/font]
[font=Arial][size=10.5pt]</Directory>[/size][/font]


[font=Arial][size=10.5pt]man[/size][/font][font=宋体][size=10.5pt]对[/size][/font][font=Arial][size=10.5pt]AllowOverride [/size][/font][font=宋体][size=10.5pt]的解释:[/size][/font]

[font=Arial][size=10.5pt]AllowOverride controls what directives may be placed in .htaccess files.[/size][/font]
[font=Arial][size=10.5pt]It can be "All", "None", or any combination of the keywords:[/size][/font]
[font=Arial][size=10.5pt]Options FileInfo AuthConfig Limit[/size][/font]


[font=宋体][size=10.5pt]就是说,将[/size][/font][font=Arial][size=10.5pt]None[/size][/font][font=宋体][size=10.5pt]改为[/size][/font][font=Arial][size=10.5pt]All[/size][/font][font=宋体][size=10.5pt],[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]文件才能被支持![/size][/font]

[size=10.5pt][font=Times New Roman]2.
[/font][/size][font=宋体][size=10.5pt]创建[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]文件[/size][/font][font=Arial][size=10.5pt]Win32 [/size][/font][font=宋体][size=10.5pt]系统下,无法直接建立[/size][/font][font=Arial][size=10.5pt] .htaccess [/size][/font][font=宋体][size=10.5pt]文件,您可以从其他系统中拷贝一份,或者在[/size][/font][font=Arial][size=10.5pt] Discuz.net [/size][/font][font=宋体][size=10.5pt]技术支持栏目中下载此文件。(附件中可下载)[/size][/font]

[size=10.5pt][font=Times New Roman]3.
[/font][/size][font=宋体][size=10.5pt]编辑[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]文件[/size][/font]

[font=Arial][size=10.5pt]# [/size][/font][font=宋体][size=10.5pt]将[/size][/font][font=Arial][size=10.5pt] RewriteEngine [/size][/font][font=宋体][size=10.5pt]模式打开[/size][/font]

[font=Arial][size=10.5pt]RewriteEngine On[/size][/font]
[font=Arial][size=10.5pt]# .htaccess[/size][/font][font=宋体][size=10.5pt]文件路径,如果在系统跟目录则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /[/size][/font][font=宋体][size=10.5pt],如果在根目录下的其他文件夹,如在根目录下的[/size][/font][font=Arial][size=10.5pt]test[/size][/font][font=宋体][size=10.5pt]文件夹,则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /test[/size][/font][font=宋体][size=10.5pt],此处将[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]放在根目录下。[/size][/font]

[font=Arial][size=10.5pt]RewriteBase /discuz[/size][/font]
[font=Arial][size=10.5pt]# Rewrite [/size][/font][font=宋体][size=10.5pt]系统规则请勿修改[/size][/font]

[font=Arial][size=10.5pt]RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^tag-(.+)\.html$ tag.php?name=$1[/size][/font]
[font=Times New Roman][/font]
[b][font=宋体]租用空间用户[/font]
[/b]
[font=Arial][size=10.5pt]1.
[/size][/font][font=宋体][size=10.5pt]首先咨询您的空间服务商,空间是否支持[/size][/font][font=Arial][size=10.5pt] Rewrite [/size][/font][font=宋体][size=10.5pt]以及是否支持对站点目录中[/size][/font][font=Arial][size=10.5pt] .htaccess [/size][/font][font=宋体][size=10.5pt]的文件解析,否则即便按照下面的方法设置好了,也无法使用。[/size][/font]

[font=Arial][size=10.5pt]2.
[/size][/font][font=宋体][size=10.5pt]创建[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]文件[/size][/font][font=Arial][size=10.5pt]Win32 [/size][/font][font=宋体][size=10.5pt]系统下,无法直接建立[/size][/font][font=Arial][size=10.5pt] .htaccess [/size][/font][font=宋体][size=10.5pt]文件,您可以从其他系统中拷贝一份,或者在[/size][/font][font=Arial][size=10.5pt] Discuz.net [/size][/font][font=宋体][size=10.5pt]技术支持栏目中下载此文件。[/size][/font]

[font=Arial][size=10.5pt]3.
[/size][/font][font=宋体][size=10.5pt]编辑[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]文件[/size][/font]

[font=Arial][size=10.5pt]# [/size][/font][font=宋体][size=10.5pt]将[/size][/font][font=Arial][size=10.5pt] RewriteEngine [/size][/font][font=宋体][size=10.5pt]模式打开[/size][/font]

[font=Arial][size=10.5pt]RewriteEngine On[/size][/font]
[font=Arial][size=10.5pt]# .htaccess[/size][/font][font=宋体][size=10.5pt]文件路径,如果在系统跟目录则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /[/size][/font][font=宋体][size=10.5pt],如果在根目录下的其他文件夹,如在根目录下的[/size][/font][font=Arial][size=10.5pt]test[/size][/font][font=宋体][size=10.5pt]文件夹,则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /test[/size][/font][font=宋体][size=10.5pt],此处将[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]放在根目录下。[/size][/font]

[font=Arial][size=10.5pt]RewriteBase /discuz[/size][/font]
[font=Arial][size=10.5pt]# Rewrite [/size][/font][font=宋体][size=10.5pt]系统规则请勿修改[/size][/font]

[font=Arial][size=10.5pt]RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^tag-(.+)\.html$ tag.php?name=$1[/size][/font]


[b][font=宋体][size=16pt]二.[/size][/font][/b][b][font=Arial][size=16pt]Rewrite [/size][/font][/b][b][font=宋体][size=16pt]规则[/size][/font][/b][b]
[/b]
[font=宋体][size=10.5pt]上面无论是在[/size][/font][font=Arial][size=10.5pt]apache[/size][/font][font=宋体][size=10.5pt]中设置的:[/size][/font]

[font=Times New Roman]<IfModule mod_Rewrite.c>[/font]
[font=Times New Roman]RewriteEngine On[/font]
[font=Times New Roman]RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2[/font]
[font=Times New Roman]RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3[/font]
[font=Times New Roman]RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2[/font]
[font=Times New Roman]</IfModule>[/font]
[font=宋体][size=10.5pt]还在是文件[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]中添加的:[/size][/font]

[font=Arial][size=10.5pt]# [/size][/font][font=宋体][size=10.5pt]将[/size][/font][font=Arial][size=10.5pt] RewriteEngine [/size][/font][font=宋体][size=10.5pt]模式打开[/size][/font]

[font=Arial][size=10.5pt]RewriteEngine On[/size][/font]
[font=Arial][size=10.5pt]# .htaccess[/size][/font][font=宋体][size=10.5pt]文件路径,如果在系统跟目录则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /[/size][/font][font=宋体][size=10.5pt],如果在根目录下的其他文件夹,如在根目录下的[/size][/font][font=Arial][size=10.5pt]test[/size][/font][font=宋体][size=10.5pt]文件夹,则为[/size][/font][font=Arial][size=10.5pt]RewriteBase /test[/size][/font][font=宋体][size=10.5pt],此处将[/size][/font][font=Arial][size=10.5pt].htaccess[/size][/font][font=宋体][size=10.5pt]放在根目录下。[/size][/font]

[font=Arial][size=10.5pt]RewriteBase /discuz[/size][/font]
[font=Arial][size=10.5pt]# Rewrite [/size][/font][font=宋体][size=10.5pt]系统规则请勿修改[/size][/font]

[font=Arial][size=10.5pt]RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2[/size][/font]
[font=Arial][size=10.5pt]RewriteRule ^tag-(.+)\.html$ tag.php?name=$1[/size][/font]
[font=宋体][size=10.5pt]其中都指明了[/size][/font][font=Arial][size=10.5pt]URL[/size][/font][font=宋体][size=10.5pt]重写规则。[/size][/font]



[font=宋体][size=10.5pt]请看:[/size][/font][font=Arial][size=10.5pt]RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2[/size][/font]
[font=宋体][size=10.5pt]这句说明了[/size][/font][font=Arial][size=10.5pt]URL[/size][/font][font=宋体][size=10.5pt]为[/size][/font][font=Arial][size=10.5pt]forumdisplay.php?fid=$1&page=$2[/size][/font][font=宋体][size=10.5pt]可以写成[/size][/font][font=Arial][size=10.5pt]forum-([0-9]+)-([0-9]+)\.html[/size][/font][font=宋体][size=10.5pt]这种模式。[/size][/font]



[font=宋体][size=10.5pt]如[/size][/font][font=宋体][size=10.5pt]:[/size][/font][font=宋体][size=10.5pt]访问[/size][/font][font=Arial][size=10.5pt]http://localhost/ forumdisplay.php?fid=1&page=2[/size][/font][font=宋体][size=10.5pt]与访问[/size][/font][font=Arial][size=10.5pt]http://localhost/ forum-1-2.html[/size][/font][font=宋体][size=10.5pt]的效果是一样的![/size][/font]



[font=宋体][size=10.5pt]注:这些规则是可以自己写正则表达式随意更改的。根据自己需要的格式。来定制[/size][/font][font=Arial][size=10.5pt]URL[/size][/font][font=宋体][size=10.5pt]重写规则。[/size][/font]


[font=宋体][size=10.5pt][font=宋体][size=10.5pt]参考资料:[/size][/font]

[url=http://faq.comsenz.com/viewnews-12][font=Times New Roman][color=#800080]http://faq.comsenz.com/viewnews-12[/color][/font][/url][font=Times New Roman]

[/font]
[url=http://www.eygle.com/digest/2005/09/apache_oeoeooaeuooa_rewrite.html][font=Times New Roman][color=#800080]http://www.eygle.com/digest/2005/09/apache_oeoeooaeuooa_rewrite.html[/color][/font][/url]
[/size][/font]

libaiyi 发表于 2008-11-16 14:54

不错顶你了。

php^_^ 发表于 2008-11-17 09:13

不错,一直是这样用的,嘿嘿

jackywdx 发表于 2008-11-17 10:12

挺好的。

handywu 发表于 2008-11-17 17:08

小梁子开始写原创了?
牛B!

handywu 发表于 2008-11-17 17:09

我一直就是按你这样做的,哈哈。。。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.