黄督师 發表於 2025-6-16 00:00:00

如何为 Discuz! X2 配置伪静态规则[超详细]

<p>URL 静态化是一个有利于搜索引擎的设置,通过 URL 静态化,达到原来是动态的 PHP 页面转换为静态化的 HTML 页面,可以提高搜索引擎抓取,当然,这里的静态化是一种假静态,目的只是提高搜索引擎的搜索量,下面主要介绍配置方法。<br />分两种情况,一种是独立主机用户,这部分用户拥有对主机的管理权限,因此配置起来比较方便一些。<br /><br />首先确定您使用的 Apache 版本,及是否加载了 mod_Rewrite 模块。<br />Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:<br />LoadModule Rewrite_module libexec/mod_Rewrite.so<br />AddModule mod_Rewrite.c Apache 2.x 的用户请检查 conf/httpd.conf 中是否存在如下一段代码:<br />LoadModule Rewrite_module modules/mod_Rewrite.so<br />如果存在,那么在配置文件(通常就是 conf/httpd.conf)中加入如下代码。此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 &lt;VirtualHost&gt; 中去,如果加在虚拟主机配置外部将可能无法使用,改好后将 Apache 重启。<br /><br /><strong>一、Apache Web Server(独立主机用户) </strong><br />&nbsp;</p>
<div class="msgheader">代码如下:</div>
<div class="msgborder" id="phpcode38"><br />&lt;IfModule mod_rewrite.c&gt;<br />RewriteEngine On<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/topic-(.+)\.html$ $1/portal.php?mod=topic&amp;topic=$2&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/forum-(\w+)-(+)\.html$ $1/forum.php?mod=forumdisplay&amp;fid=$2&amp;page=$3&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/thread-(+)-(+)-(+)\.html$ $1/forum.php?mod=viewthread&amp;tid=$2&amp;extra=page\%<br />3D$4&amp;page=$3&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/group-(+)-(+)\.html$ $1/forum.php?mod=group&amp;fid=$2&amp;page=$3&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&amp;$2=$3&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^(.*)/(fid|tid)-(+)\.html$ $1/index.php?action=$2&amp;value=$3&amp;%1<br />&lt;/IfModule&gt;</div>
<p></p>
<p><br />如果没有安装 mod_Rewrite,您可以重新编译 Apache,并在原有 configure 的内容中加入 --enable-Rewrite=shared,然后再在Apache 配置文件中加入上述代码即可。<br /><br /><strong>二、Apache Web Server(虚拟主机用户) </strong><br /><br />在开始以下设置之前,请首先咨询您的空间服务商,空间是否支持 Rewrite 以及是否支持对站点目录中 .htaccess 的文件解析,否则即便按照下面的方法设置好了,也无法使用。<br />检查论坛所在目录中是否存在 .htaccess 文件,如果不存在,请手工建立此文件。Win32 系统下,无法直接建立 .htaccess 文件,您可以从其他系统中拷贝一份,编辑并修改 .htaccess 文件,添加以下内容:</p>
<p></p>
<p></p>
<p></p>
<p></p>
<div class="msgheader">代码如下:</div>
<p></p>
<p></p>
<p></p>
<div class="msgborder" id="phpcode39"><br /># 将 RewriteEngine 模式打开<br />RewriteEngine On<br /># 修改以下语句中的 /discuz 为你的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /<br />RewriteBase /discuz<br /># Rewrite 系统规则请勿修改<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&amp;topic=$1&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^forum-(\w+)-(+)\.html$ forum.php?mod=forumdisplay&amp;fid=$1&amp;page=$2&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^thread-(+)-(+)-(+)\.html$ forum.php?mod=viewthread&amp;tid=$1&amp;extra=page\%3D$3&amp;page=$2&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^group-(+)-(+)\.html$ forum.php?mod=group&amp;fid=$1&amp;page=$2&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&amp;$1=$2&amp;%1<br />RewriteCond %{QUERY_STRING} ^(.*)$<br />RewriteRule ^archiver/(fid|tid)-(+)\.html$ archiver/index.php?action=$1&amp;value=$2&amp;%1</div>
<p><br /><strong>三、IIS Web Server(独立主机用户) </strong><br /><br />说明:IIS 下的 Rewrite 需要添加组件,通过筛选器的方式实现,下面是组件的下载与配置方法:<br />IIS Rewrite 下载 Rewrite.zip <br /><br />IIS Rewrite 配置<br />1、将下载的 IIS Rewrite 组件解压,放到任意盘上的任意目录(如 C:Rewrite 下)<br /><img style="max-width:100%!important;height:auto!important;"alt="1.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-2506160940494R.gif" width="427" />&nbsp;<br /><br />2、在 IIS 管理器里选择网站,右键选择&ldquo;属性&rdquo;,如下图所示:<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="2.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094049E5.gif" width="368" />&nbsp;<br /><br />在弹出的窗口里选择&ldquo;ISAPI筛选器&rdquo;<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="3.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094049139.gif" width="473" />&nbsp;<br /><br />上图中点击&ldquo;添加&rdquo;,在弹出的窗口里&ldquo;筛选器名称&rdquo;填写&ldquo;rewrite&rdquo;<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="4.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094050527.gif" width="475" />&nbsp;<br /><br />上图界面中点击&ldquo;浏览&rdquo;,选择下载解压后的 IIS Rewrite 组件目录下的 Rewrite.dll 文件<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="" height="483" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094050939.gif" width="600" />&nbsp;<br /><br />浏览完毕点击&ldquo;确定&rdquo;<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="6.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094050K4.gif" width="476" />&nbsp;<br /><br />添加完毕点击&ldquo;确定<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="7.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094050529.gif" width="477" />&nbsp;<br /><br />重新启动 IIS<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="8.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094051D7.gif" width="479" /></p>
<div class="aimg_tip tip tip_4" id="aimg_964506_menu" style="display:none; left:412px; position:absolute; top:5755px; z-index:301"></div>
<p></p>
<p><br /><img style="max-width:100%!important;height:auto!important;"alt="9.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094051124.gif" width="379" /><br />重新选择该站点 =&gt; 右键&ldquo;属性&rdquo;=&gt; &ldquo;ISAPI 筛选器&rdquo;,如果看到状态为向上的绿色箭头,就说明 Rewrite 模块安装成功了。<br /><br /><img style="max-width:100%!important;height:auto!important;"alt="10.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094051934.gif" width="475" />&nbsp;<br /><br />这个时候返回到论坛首页随便打开一个版块或者一个帖子即可看到 Discuz! 的 Rewrite 伪静态配置成功!<br /><img style="max-width:100%!important;height:auto!important;"alt="11.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-2506160940514X.gif" width="396" /><br /><br /><img style="max-width:100%!important;height:auto!important;"alt="12.gif" src="https://zhuji.jb51.net/uploads/allimg/20250616/1-250616094051c4.gif" width="394" />&nbsp;<br />IIS Web Server(独立主机用户)</p>
<p></p>
<p></p>
<p></p>
<div class="msgheader">代码如下:</div>
<p></p>
<p></p>
<div class="msgborder" id="phpcode40"><br /><br /># 3600 = 1 hour<br />CacheClockRate 3600<br />RepeatLimit 32<br /># Protect httpd.ini and httpd.parse.errors files<br /># from accessing through HTTP<br />RewriteRule ^(.*)/topic-(.+)\.html(\?(.*))*$ $1/portal\.php\?mod=topic&amp;topic=$2&amp;$4<br />RewriteRule ^(.*)/article-(+)-(+)\.html(\?(.*))*$ $1/portal\.php\?mod=view&amp;aid=$2&amp;page=$3&amp;$5<br />RewriteRule ^(.*)/forum-(\w+)-(+)\.html(\?(.*))*$ $1/forum\.php\?mod=forumdisplay&amp;fid=$2&amp;page=$3&amp;$5<br />RewriteRule ^(.*)/thread-(+)-(+)-(+)\.html(\?(.*))*$ $1/forum\.php\?mod=viewthread&amp;tid=$2&amp;extra=page\%3D$4&amp;page=$3&amp;$6<br />RewriteRule ^(.*)/group-(+)-(+)\.html(\?(.*))*$ $1/forum\.php\?mod=group&amp;fid=$2&amp;page=$3&amp;$5<br />RewriteRule ^(.*)/space-(username|uid)-(.+)\.html(\?(.*))*$ $1/home\.php\?mod=space&amp;$2=$3&amp;$5<br />RewriteRule ^(.*)/blog-(+)-(+)\.html(\?(.*))*$ $1/home\.php\?mod=space&amp;uid=$2&amp;do=blog&amp;id=$3&amp;$5<br />RewriteRule ^(.*)/(fid|tid)-(+)\.html(\?(.*))*$ $1/index\.php\?action=$2&amp;value=$3&amp;$5</div>
<p><br /><strong>四、IIS7 Web Server(独立主机用户) </strong><br /><br />可以将如下的配置规则加入到 web.config 中</p>
<p></p>
<p></p>
<div class="msgheader">代码如下:</div>
<p></p>
<p></p>
<div class="msgborder" id="phpcode41"><br />&lt;rewrite&gt;<br />&lt;rules&gt;<br />&lt;rule name=&quot;portal_topic&quot;&gt;<br />&lt;match url=&quot;^(.*/)*topic-(.+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/portal.php\?mod=topic&amp;amp;topic={R:2}&amp;amp;{R:3}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;rule name=&quot;forum_forumdisplay&quot;&gt;<br />&lt;match url=&quot;^(.*/)*forum-(\w+)-(+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/forum.php\?mod=forumdisplay&amp;amp;fid={R:2}&amp;amp;page={R:3}&amp;amp;{R:4}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;rule name=&quot;forum_viewthread&quot;&gt;<br />&lt;match url=&quot;^(.*/)*thread-(+)-(+)-(+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/forum.php\?mod=viewthread&amp;amp;tid={R:2}&amp;amp;extra=page%3D{R:4}&amp;amp;page={R:3}&amp;amp;{R:5}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;rule name=&quot;group_group&quot;&gt;<br />&lt;match url=&quot;^(.*/)*group-(+)-(+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/forum.php\?mod=group&amp;amp;fid={R:2}&amp;amp;page={R:3}&amp;amp;{R:4}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;rule name=&quot;home_space&quot;&gt;<br />&lt;match url=&quot;^(.*/)*space-(username|uid)-(.+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/home.php\?mod=space&amp;amp;{R:2}={R:3}&amp;amp;{R:4}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;rule name=&quot;forum_archiver&quot;&gt;<br />&lt;match url=&quot;^(.*/)*(fid|tid)-(+).html\?*(.*)$&quot; /&gt;<br />&lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}/index.php\?action={R:2}&amp;amp;value={R:3}&amp;amp;{R:4}&quot; /&gt;<br />&lt;/rule&gt;<br />&lt;/rules&gt;<br />&lt;/rewrite&gt;</div>
<p><br /><strong>五、Zeus Web Server </strong><br /><br />配置规则如下:</p>
<p></p>
<p></p>
<div class="msgheader">代码如下:</div>
<p></p>
<p></p>
<div class="msgborder" id="phpcode42"><br />match URL into $ with ^(.*)/topic-(.+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/portal.php?mod=topic&amp;topic=$2&amp;$3<br />endif<br />match URL into $ with ^(.*)/forum-(\w+)-(+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/forum.php?mod=forumdisplay&amp;fid=$2&amp;page=$3&amp;$4<br />endif<br />match URL into $ with ^(.*)/thread-(+)-(+)-(+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/forum.php?mod=viewthread&amp;tid=$2&amp;extra=page\%3D$4&amp;page=$3&amp;$5<br />endif<br />match URL into $ with ^(.*)/group-(+)-(+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/forum.php?mod=group&amp;fid=$2&amp;page=$3&amp;$4<br />endif<br />match URL into $ with ^(.*)/space-(username|uid)-(.+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/home.php?mod=space&amp;$2=$3&amp;$4<br />endif<br />match URL into $ with ^(.*)/(fid|tid)-(+)\.html\?*(.*)$<br />if matched then<br />set URL = $1/index.php?action=$2&amp;value=$3&amp;$4<br />endif</div>
<p><br /><strong>六、Nginx Web Server </strong><br /><br />配置规则如下:</p>
<p></p>
<p></p>
<div class="msgheader">代码如下:</div>
<p></p>
<p></p>
<div class="msgborder" id="phpcode43"><br />rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&amp;topic=$2 last;<br />rewrite ^([^\.]*)/forum-(\w+)-(+)\.html$ $1/forum.php?mod=forumdisplay&amp;fid=$2&amp;page=$3 last;<br />rewrite ^([^\.]*)/thread-(+)-(+)-(+)\.html$ $1/forum.php?mod=viewthread&amp;tid=$2&amp;extra=page%<br />3D$4&amp;page=$3 last;<br />rewrite ^([^\.]*)/group-(+)-(+)\.html$ $1/forum.php?mod=group&amp;fid=$2&amp;page=$3 last;<br />rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&amp;$2=$3 last;<br />rewrite ^([^\.]*)/(fid|tid)-(+)\.html$ $1/index.php?action=$2&amp;value=$3 last;<br />if (!-e $request_filename) {<br />return 404;<br />}</div>
<div class="clearfix"></div>
頁: [1]
查看完整版本: 如何为 Discuz! X2 配置伪静态规则[超详细]