2025-04-23

本文目錄1IIS偽靜態規則2Apache偽靜態規則3Nginx偽靜態規則

不少朋友總是詢問 WordPress 如何添加偽靜態規則,今天就總結一下 IIS/Apache/Nginx 三種環境下的偽靜態規則,希望對大傢有所幫助。

檢測主機是否支援偽靜態的方法:在WP後臺 > 設置 > 固定鏈接,設置為 默認帶?的那種結構,然後訪問任何一篇文章,如果出現 404 錯誤,說明你的主機當前不支援 WordPress 偽靜態。

IIS偽靜態規則

IIS 環境是 Windows 主機常用的服務器環境,新建一個 txt 文件,將下面的代碼添加到文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[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]

[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 文件,添加下面的代碼:

1
2
3
4
5
6
7
8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<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   { } 大括號裡面添加下面的代碼:

1
2
3
4
5
6
location / {
	try_files $uri $uri/ /index.php?$args;
}
 
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location / {
try_files $uri $uri/ /index.php?$args;
} # Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

保存,重啟 Nginx 即可。

註:如果以上規則不適用,請自己查看WP官方文檔:https://codex.wordpress.org/Nginx

題外話:一直不推薦在 windows 的IIS服務器下安裝 WordPress,因為 IIS 環境運行php程式的效率,相對同等配置下 Linux 的 Apache 和 Nginx 環境,要低的多,具體可以看看https://www.wpdaxue.com/wordpress-hosting.html

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *