APACHE通过URL重写伪静态

一.Apache设置

独立主机用户

Apache 基本配置:

首先确定您使用的 Apache 版本,及是否加载了 mod_Rewrite 模块。

Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:

LoadModule Rewrite_module libexec/mod_Rewrite.so

AddModule mod_Rewrite.c

Apache 2.x 的用户请检查 conf/httpd.conf 中是否存在如下一段代码:

LoadModule Rewrite_module modules/mod_Rewrite.so

如果没有安装 mod_Rewrite,您可以重新编译 Apache,并在原有 configure 的内容中加入 --enable-Rewrite=shared

注:如果前面有#,将其去掉。

方法一:通过配置Apache配置文件httpd.conf实现URL重写

在配置文件(通常就是 conf/httpd.conf)中加入如下代码。

<IfModule mod_Rewrite.c>

RewriteEngine On

RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2

RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3

RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3

RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3

RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2

</IfModule>

注:此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 <VirtualHost> 中去,如果加在虚拟主机配置外部将可能无法使用,改好后将 Apache 重启。

方法二:通过在根目录中的跨越配置文件.htaccess实现URL重写

1.

配置apache支持对 .htaccess 文件的解析

查找:

<Directory />

Options FollowSymLinks

AllowOverride None

</Directory>

修改为:

<Directory />

Options FollowSymLinks

AllowOverride All

</Directory>

man对AllowOverride 的解释:

AllowOverride controls what directives may be placed in .htaccess files.

It can be "All", "None", or any combination of the keywords:

Options FileInfo AuthConfig Limit

就是说,将None改为All,.htaccess文件才能被支持!

2. 创建.htaccess文件Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,或者在 Discuz.net 技术支持栏目中下载此文件。(附件中可下载)

3. 编辑.htaccess文件

# 将 RewriteEngine 模式打开

RewriteEngine On

# .htaccess文件路径,如果在系统跟目录则为RewriteBase /,如果在根目录下的其他文件夹,如在根目录下的test文件夹,则为RewriteBase /test,此处将.htaccess放在根目录下。

RewriteBase /discuz

# Rewrite 系统规则请勿修改

RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1

RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2

RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2

RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2

RewriteRule ^tag-(.+)\.html$ tag.php?name=$1

租用空间用户(多是虚拟主机用户)

1. 首先咨询您的空间服务商,空间是否支持 Rewrite 以及是否支持对站点目录中 .htaccess 的文件解析,否则即便按照下面的方法设置好了,也无法使用。

2. 创建.htaccess文件Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,或者在 Discuz.net 技术支持栏目中下载此文件。

3. 编辑.htaccess文件

# 将 RewriteEngine 模式打开

RewriteEngine On

# .htaccess文件路径,如果在系统跟目录则为RewriteBase /,如果在根目录下的其他文件夹,如在根目录下的test文件夹,则为RewriteBase /test,此处将.htaccess放在根目录下。

RewriteBase /discuz

# Rewrite 系统规则请勿修改

RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1

RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2

RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2

RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2

RewriteRule ^tag-(.+)\.html$ tag.php?name=$1

二.Rewrite 规则

上面无论是在apache中设置的:

<IfModule mod_Rewrite.c>

RewriteEngine On

RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2

RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3

RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3

RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3

RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2

</IfModule>

还在是文件.htaccess中添加的:

# 将 RewriteEngine 模式打开

RewriteEngine On

# .htaccess文件路径,如果在系统跟目录则为RewriteBase /,如果在根目录下的其他文件夹,如在根目录下的test文件夹,则为RewriteBase /test,此处将.htaccess放在根目录下。

RewriteBase /discuz

# Rewrite 系统规则请勿修改

RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1

RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2

RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2

RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2

RewriteRule ^tag-(.+)\.html$ tag.php?name=$1

其中都指明了URL重写规则。

请看:RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2

这句说明了URL为forumdisplay.php?fid=$1&page=$2可以写成forum-([0-9]+)-([0-9]+)\.html这种模式。

如:访问http://localhost/ forumdisplay.php?fid=1&page=2与访问http://localhost/ forum-1-2.html的效果是一样的!

注:这些规则是可以自己写正则表达式随意更改的。根据自己需要的格式。来定制URL重写规则。

参考资料:

http://faq.comsenz.com/viewnews-12

http://www.eygle.com/digest/2005/09/apache_oeoeooaeuooa_rewrite.html

posted @ 2009-11-24 10:33  般若随风  阅读(11390)  评论(0编辑  收藏  举报