不少朋友總是詢問 WordPress 如何新增偽靜態規則,今天幻殺就總結一下 IIS6/IIS7.5/Apache/Nginx/win2003/win2008 四種環境下的偽靜態規則,希望對大家有所幫助。其中 IIS7.5 的規則是目前最完美的哦
明明是六種為什麼要說是四種呢?其實 windows 的主機在 2003 時為 IIS6,而 2008 以後升級為了 IIS7.5 所以 IIS6 的規則在 IIS7.5 不通用哦,其實我以前也很糾結的。弄了半天規則才發現,悲催了
檢測主機是否支援偽靜態的方法:在 WP 後臺 > 設定 > 固定連結,設定為 非預設帶? 的那種結構,然後訪問任何一篇文章,如果出現 404 錯誤,說明你的主機當前不支援 WordPress 偽靜態。

IIS7.5 完美偽靜態規則
IIS 環境是 Windows 主機常用的伺服器環境,但網上很多規則其實並不完美,很多地方依舊有些無法設定的地方,但這個卻可以,新建兩個 txt 檔案,將下面的程式碼分別新增到檔案中:
第一個 web.config
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ChineseURL" stopProcessing="true">
<match url="http://(tag|category)/(.*)$" />
<action type="Rewrite" url="ihuanurl.php"/>
</rule>
<rule name="WordPress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
第二個輔助檔案 ihuanurl.php:
<?php
// IIS Mod-Rewrite
if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
}
// IIS Isapi_Rewrite
else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
}
else
{
// Use ORIG_PATH_INFO if there is no PATH_INFO
if ( !isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO']) )
$_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
// Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
if ( isset($_SERVER['PATH_INFO']) ) {
if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
else
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
}
// Append the query string if it exists and isn't null
if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}
require("index.php");
?>
然後儲存檔案,上傳到 WordPress 站點的根目錄即可。
IIS6 偽靜態規則
IIS 環境是 Windows 主機常用的伺服器環境,新建一個 txt 檔案,將下面的程式碼新增到檔案中:
[ISAPI_Rewrite] # Defend your computer from some worm attacks #RewriteRule .*(?:global.asa|default.ida|root.exe|..).* . [F,I,O] # 3600 = 1 hour CacheClockRate 3600 RepeatLimit 32 # Protect httpd.ini and httpd.parse.errors files # from accessing through HTTP # Rules to ensure that normal content gets through RewriteRule /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 即可。
最後來個結尾:你們會問我我為什麼知道 IIS7.5 的規則,其實以前我就是用的 win2008 的機子,但是用來做部落格的話,win 主機真的不大適合,絕大部分運營商都會禁止 smtp 的,而且 win 主機執行 php 實在是太慢了,推薦在 win 的主機上安裝阿帕奇之後再用,真的,我不想深受其害了