不少朋友總是詢問 WordPress 如何添加偽靜態規則,今天珞凡就總結一下 IIS/Apache/Nginx 三種環境下的偽靜態規則,希望對大家有所幫助。檢測主機是否支持偽靜態的方法:在 WP 後台 > 設置 > 固定鏈接,設置自定義結構,然後訪問任何一篇文章,如果出現 404 錯誤,説明你的主機當前不支持 WordPress 偽靜態。
IIS 偽靜態規則
IIS 環境是 Windows 主機常用的服務器環境,新建一個 txt 文件,將下面的代碼添加到文件中:
[ISAPI_Rewrite]
# Defend your computer from some worm attacks
#RewriteRule .*(?:global.asa|default.ida|root.exe|..).* . [F,I,O]
# 3600 = 1 hourCacheClockRate 3600
RepeatLimit 32# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP
# Rules to ensure that normal content gets throughRewriteRule /tag/(.*) /index.php?tag=$1
RewriteRule /software-files/(.*) /software-files/$1 [L]
RewriteRule /images/(.*) /images/$1 [L]
RewriteRule /sitemap.xml /sitemap.xml [L]
RewriteRule /favicon.ico /favicon.ico [L]
# For file-based wordpress content (i.e. theme), admin, etc.
RewriteRule /wp-(.*) /wp-$1 [L]
# For normal wordpress content, via index.php
RewriteRule ^/$ /index.php [L]
RewriteRule /(.*) /index.php/$1 [L]
然後另存為 httpd.ini 文件,上傳到 WordPress 站點的根目錄即可。
Apache 偽靜態規則
Apache 是 Linux 主機下常見的環境,現在一般的 Linux 虛擬主機都採用這種環境。新建一個 htaccess.txt 文件,添加下面的代碼:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
然後上傳到 WordPress 站點的根目錄,重命名為 .htaccess 即可。
Nginx 偽靜態規則
Nginx 環境一般是 Linux 主機 VPS 或服務器用户用的比較多,這些用户一般都會自己配置 Nginx,或者有專門的人幫你配置,打開 nginx.conf 或者某個站點的配置環境,比如 wpdaxue.com.conf(不同人配置的不一樣),在 server { } 大括號裏面添加下面的代碼:
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
保存,重啓 Nginx 即可。
題外話:珞凡不推薦在 windows 的 IIS 服務器下安裝 WordPress,因為 IIS 環境運行 php 程序的效率,相對同等配置下 Linux 的 Apache 和 Nginx 環境,要低的多。實踐證明,Linux 主機的 PHP 環境更加有利於高效運行 WordPress 等 PHP 程序,比如對偽靜態的支持、對中文鏈接的支持,對其他 WordPress 所需函數的支持更加完美,運行更加流暢。如果你使用 windows 主機,你會發現,運行 Wordpress 感覺比較慢,而且通常不能完美支持偽靜態,而且網址中有中文的話,就會出現 404 錯誤,有時候還沒辦法使用某些插件……