Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

如何为 Discuz! X2 配置伪静态

  [复制链接]
玻璃屋女孩 发表于 2011-6-7 14:50:27 | 显示全部楼层 |阅读模式
本帖最后由 玻璃屋女孩 于 2011-6-7 15:45 编辑

URL 静态化是一个有利于搜索引擎的设置,通过 URL 静态化,达到原来是动态的 PHP 页面转换为静态化的 HTML 页面,可以提高搜索引擎抓取,当然,这里的静态化是一种假静态,目的只是提高搜索引擎的搜索量,下面主要介绍配置方法。
分两种情况,一种是独立主机用户,这部分用户拥有对主机的管理权限,因此配置起来比较方便一些。

首先确定您使用的 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
如果存在,那么在配置文件(通常就是 conf/httpd.conf)中加入如下代码。此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 <VirtualHost> 中去,如果加在虚拟主机配置外部将可能无法使用,改好后将 Apache 重启。

一、Apache Web Server(独立主机用户)

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteCond %{QUERY_STRING} ^(.*)$
  4. RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2&%1
  5. RewriteCond %{QUERY_STRING} ^(.*)$
  6. RewriteRule ^(.*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
  7. RewriteCond %{QUERY_STRING} ^(.*)$
  8. RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page\%
  9. 3D$4&page=$3&%1
  10. RewriteCond %{QUERY_STRING} ^(.*)$
  11. RewriteRule ^(.*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3&%1
  12. RewriteCond %{QUERY_STRING} ^(.*)$
  13. RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3&%1
  14. RewriteCond %{QUERY_STRING} ^(.*)$
  15. RewriteRule ^(.*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3&%1
  16. </IfModule>
复制代码


如果没有安装 mod_Rewrite,您可以重新编译 Apache,并在原有 configure 的内容中加入 --enable-Rewrite=shared,然后再在Apache 配置文件中加入上述代码即可。

二、Apache Web Server(虚拟主机用户)

在开始以下设置之前,请首先咨询您的空间服务商,空间是否支持 Rewrite 以及是否支持对站点目录中 .htaccess 的文件解析,否则即便按照下面的方法设置好了,也无法使用。
检查论坛所在目录中是否存在 .htaccess 文件,如果不存在,请手工建立此文件。Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,编辑并修改 .htaccess 文件,添加以下内容:


  1. # 将 RewriteEngine 模式打开
  2. RewriteEngine On
  3. # 修改以下语句中的 /discuz 为你的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
  4. RewriteBase /discuz
  5. # Rewrite 系统规则请勿修改
  6. RewriteCond %{QUERY_STRING} ^(.*)$
  7. RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
  8. RewriteCond %{QUERY_STRING} ^(.*)$
  9. RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
  10. RewriteCond %{QUERY_STRING} ^(.*)$
  11. RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
  12. RewriteCond %{QUERY_STRING} ^(.*)$
  13. RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
  14. RewriteCond %{QUERY_STRING} ^(.*)$
  15. RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
  16. RewriteCond %{QUERY_STRING} ^(.*)$
  17. RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
复制代码


三、IIS Web Server(独立主机用户)

说明:IIS 下的 Rewrite 需要添加组件,通过筛选器的方式实现,下面是组件的下载与配置方法:
IIS Rewrite 下载

IIS Rewrite 配置
1、将下载的 IIS Rewrite 组件解压,放到任意盘上的任意目录(如 C:Rewrite 下)



2、在 IIS 管理器里选择网站,右键选择“属性”,如下图所示:



在弹出的窗口里选择“ISAPI筛选器”



上图中点击“添加”,在弹出的窗口里“筛选器名称”填写“rewrite”



上图界面中点击“浏览”,选择下载解压后的 IIS Rewrite 组件目录下的 Rewrite.dll 文件



浏览完毕点击“确定”



添加完毕点击“确定



重新启动 IIS





重新选择该站点 => 右键“属性”=> “ISAPI 筛选器”,如果看到状态为向上的绿色箭头,就说明 Rewrite 模块安装成功了。



这个时候返回到论坛首页随便打开一个版块或者一个帖子即可看到 Discuz! 的 Rewrite 伪静态配置成功!




四、IIS7 Web Server(独立主机用户)

可以将如下的配置规则加入到 web.config 中


  1. <rewrite>
  2. <rules>
  3.   <rule name="portal_topic">
  4.    <match url="^(.*/)*topic-(.+).html\?*(.*)$" />
  5.    <action type="Rewrite" url="{R:1}/portal.php\?mod=topic&amp;topic={R:2}&amp;{R:3}" />
  6.   </rule>
  7.   <rule name="forum_forumdisplay">
  8.    <match url="^(.*/)*forum-(\w+)-([0-9]+).html\?*(.*)$" />
  9.    <action type="Rewrite" url="{R:1}/forum.php\?mod=forumdisplay&amp;fid={R:2}&amp;page={R:3}&amp;{R:4}" />
  10.   </rule>
  11.   <rule name="forum_viewthread">
  12.    <match url="^(.*/)*thread-([0-9]+)-([0-9]+)-([0-9]+).html\?*(.*)$" />
  13.    <action type="Rewrite" url="{R:1}/forum.php\?mod=viewthread&amp;tid={R:2}&amp;extra=page%3D{R:4}&amp;page={R:3}&amp;{R:5}" />
  14.   </rule>
  15.   <rule name="group_group">
  16.    <match url="^(.*/)*group-([0-9]+)-([0-9]+).html\?*(.*)$" />
  17.    <action type="Rewrite" url="{R:1}/forum.php\?mod=group&amp;fid={R:2}&amp;page={R:3}&amp;{R:4}" />
  18.   </rule>
  19.   <rule name="home_space">
  20.    <match url="^(.*/)*space-(username|uid)-(.+).html\?*(.*)$" />
  21.    <action type="Rewrite" url="{R:1}/home.php\?mod=space&amp;{R:2}={R:3}&amp;{R:4}" />
  22.   </rule>
  23.   <rule name="forum_archiver">
  24.    <match url="^(.*/)*(fid|tid)-([0-9]+).html\?*(.*)$" />
  25.    <action type="Rewrite" url="{R:1}/index.php\?action={R:2}&amp;value={R:3}&amp;{R:4}" />
  26.   </rule>
  27. </rules>
  28. </rewrite>
复制代码


五、Zeus Web Server

配置规则如下:


  1. match URL into $ with ^(.*)/topic-(.+)\.html\?*(.*)$
  2. if matched then
  3. set URL = $1/portal.php?mod=topic&topic=$2&$3
  4. endif
  5. match URL into $ with ^(.*)/forum-(\w+)-([0-9]+)\.html\?*(.*)$
  6. if matched then
  7. set URL = $1/forum.php?mod=forumdisplay&fid=$2&page=$3&$4
  8. endif
  9. match URL into $ with ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html\?*(.*)$
  10. if matched then
  11. set URL = $1/forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&$5
  12. endif
  13. match URL into $ with ^(.*)/group-([0-9]+)-([0-9]+)\.html\?*(.*)$
  14. if matched then
  15. set URL = $1/forum.php?mod=group&fid=$2&page=$3&$4
  16. endif
  17. match URL into $ with ^(.*)/space-(username|uid)-(.+)\.html\?*(.*)$
  18. if matched then
  19. set URL = $1/home.php?mod=space&$2=$3&$4
  20. endif
  21. match URL into $ with ^(.*)/(fid|tid)-([0-9]+)\.html\?*(.*)$
  22. if matched then
  23. set URL = $1/index.php?action=$2&value=$3&$4
  24. endif
复制代码


六、Nginx Web Server

配置规则如下:

  1. rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
  2. rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
  3. rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%
  4. 3D$4&page=$3 last;
  5. rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
  6. rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
  7. rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
  8. if (!-e $request_filename) {
  9. return 404;
  10. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

3

查看全部评分

bjhuae 发表于 2011-6-7 16:07:46 | 显示全部楼层
好帖,详细又好用,代表人民感谢一下

评分

1

查看全部评分

回复

使用道具 举报

问道外传 发表于 2011-6-7 17:03:24 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

德清 发表于 2011-6-7 21:10:24 | 显示全部楼层
不错!!我成功了 哈哈
回复

使用道具 举报

wermnb 发表于 2011-6-8 00:25:17 | 显示全部楼层
终于出来了 试试········
回复

使用道具 举报

dezhengch 发表于 2011-6-8 08:37:32 | 显示全部楼层

名师阁  http://www.mingshige.org/    支持下
回复

使用道具 举报

huifa 发表于 2011-6-8 08:49:38 | 显示全部楼层


为什么我重启IIS后还是没得,求知

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

heroabcdef 发表于 2011-6-10 12:08:58 | 显示全部楼层
哎呀,好帖啊,找的我好辛苦啊!
回复

使用道具 举报

qq83896 发表于 2011-6-10 22:28:42 | 显示全部楼层
这么好的帖子!怎么能不顶!!!!!太细心了!
回复

使用道具 举报

robbiewang 发表于 2011-6-11 10:22:18 | 显示全部楼层
看不明白:

修改以下语句中的 /discuz 为你的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
RewriteBase /discuz

如果我的网站为www.mydomain.com ,论坛都在根目录,下面的代码如何改啊?还没有想通啊

# 将 RewriteEngine 模式打开
RewriteEngine On
# 修改以下语句中的 /discuz 为你的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
RewriteBase /discuz
# Rewrite 系统规则请勿修改
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-4-24 10:19 , Processed in 0.146211 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表