偽靜態的服務器配置-如何php為 Discuz! X2 配置偽靜態


  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 下載 Rewrite.zip (100.13 KB, 下載次數: 35343)

2011-6-7 14:45 上傳

點擊文件名下載附件

IIS Rewrite 配置
1、將下載的 IIS Rewrite 組件解壓,放到任意盤上的任意目錄(如 C:Rewrite 下)

1.gif (8.66 KB, 下載次數: 528)

下載附件 保存到相冊

2011-6-7 14:48 上傳

2、在 IIS 管理器里選擇網站,右鍵選擇“屬性”,如下圖所示:

2.gif (15.94 KB, 下載次數: 386)

下載附件 保存到相冊

2011-6-7 14:48 上傳

在彈出的窗口里選擇“ISAPI篩選器”

3.gif (11.33 KB, 下載次數: 393)

下載附件 保存到相冊

2011-6-7 14:48 上傳

上圖中點擊“添加”,在彈出的窗口里“篩選器名稱”填寫“rewrite”

4.gif (11.97 KB, 下載次數: 370)

下載附件 保存到相冊

2011-6-7 14:48 上傳

上圖界面中點擊“瀏覽”,選擇下載解壓后的 IIS Rewrite 組件目錄下的 Rewrite.dll 文件

5.gif (17.05 KB, 下載次數: 327)

下載附件 保存到相冊

2011-6-7 14:48 上傳

瀏覽完畢點擊“確定”

6.gif (12.07 KB, 下載次數: 365)

下載附件 保存到相冊

2011-6-7 14:48 上傳

添加完畢點擊“確定

7.gif (12.17 KB, 下載次數: 384)

下載附件 保存到相冊

2011-6-7 14:48 上傳

重新啟動 IIS

8.gif (13.28 KB, 下載次數: 372)

下載附件 保存到相冊

2011-6-7 14:48 上傳

9.gif (7.91 KB, 下載次數: 357)

下載附件 保存到相冊

2011-6-7 14:48 上傳

重新選擇該站點 => 右鍵“屬性”=> “ISAPI 篩選器”,如果看到狀態為向上的綠色箭頭,就說明 Rewrite 模塊安裝成功了。

10.gif (12.06 KB, 下載次數: 373)

下載附件 保存到相冊

2011-6-7 14:48 上傳

這個時候返回到論壇首頁隨便打開一個版塊或者一個帖子即可看到 Discuz! 的 Rewrite 偽靜態配置成功!

11.gif (12.28 KB, 下載次數: 310)

下載附件 保存到相冊

2011-6-7 14:48 上傳

12.gif (12.47 KB, 下載次數: 340)

下載附件 保存到相冊

2011-6-7 14:48 上傳

四、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. }

復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM