我們使用 WordPress 建站時,一般都是在 IIS/Apache/Nginx 這三種環境下搭建的,若你不知道自己 WordPress 所在的環境是什麼,那麼請去詢問客服。

WordPress 偽靜態之一:Nginx 偽靜態規則

在 linux 系統中,Nginx 環境使用的人很多,如今市面上的 linux 主機 VPS 或伺服器使用者都在用這個,這些人一般都會自己配置配置 Nginx,或者有專門的人幫你配置,你只需要開啟 nginx.conf 或者對應站點的配置檔案,如 junzibuqi.com.conf(請根據你自己的來),在 conf 檔案的 server{ } 大括號裡面新增下面的程式碼:

  1. location /{
  2. if(-f $request_filename/index.html){
  3. rewrite (.*) $1/index.html break;
  4. }
  5. if(-f $request_filename/index.php){
  6. rewrite (.*) $1/index.php;
  7. }
  8. if(!-f $request_filename){
  9. rewrite (.*)/index.php;
  10. }
  11. }

將以上內容放入後記得儲存,然後重啟 Nginx 就可以使得你 WordPress 偽靜態規則生效了。

WordPress 偽靜態之一:Apache 偽靜態

比起 Nginx 的偽靜態,設定 Apache 的偽靜態規則更簡單,你只需要新建一個 htaccess.txt 檔案,新增下面的程式碼:

  1. RewriteEngineOn
  2. RewriteBase/
  3. RewriteRule^index.php$ -[L]
  4. RewriteCond%{REQUEST_FILENAME}!-f
  5. RewriteCond%{REQUEST_FILENAME}!-d
  6. RewriteRule./index.php [L]

儲存之後,就將 htaccess.txt 檔案上傳到你 WordPress 網站的根目錄,重新命名為 .htaccess 即可生效。

WordPress 偽靜態之一:IIS 偽靜態規則

IIS 環境是 Windows 主機常用的伺服器環境,直接新建一個 txt 檔案,將下面的程式碼新增到檔案中:

  1. [ISAPI_Rewrite]
  2. # Defend your computer from some worm attacks
  3. #RewriteRule .*(?:global.asa|default.ida|root.exe|..).* . [F,I,O]
  4. # 3600 = 1 hour
  5. CacheClockRate3600
  6. RepeatLimit32
  7. # Protect httpd.ini and httpd.parse.errors files
  8. # from accessing through HTTP
  9. # Rules to ensure that normal content gets through
  10. RewriteRule/tag/(.*)/index.php?tag=$1
  11. RewriteRule/software-files/(.*)/software-files/$1 [L]
  12. RewriteRule/images/(.*)/images/$1 [L]
  13. RewriteRule/sitemap.xml /sitemap.xml [L]
  14. RewriteRule/favicon.ico /favicon.ico [L]
  15. # For file-based WordPress content (i.e. theme), admin, etc.
  16. RewriteRule/wp-(.*)/wp-$1 [L]
  17. # For normal WordPress content, via index.php
  18. RewriteRule^/$ /index.php [L]
  19. RewriteRule/(.*)/index.php/$1 [L]

儲存之後, httpd.ini 檔案,上傳到 WordPress 站點的根目錄即可生效。