不少朋友總是詢問 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 的主機上安裝阿帕奇之後再用,真的,我不想深受其害了