不少朋友總是詢問 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 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 / { try_files $uri $uri/ /index.php?$args; } # Add trailing slash to */wp-admin requests. rewrite /wp-admin$ $scheme://$host$uri/ permanent;
保存,重啟 Nginx 即可。