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="^(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']) && !emptyempty($_SERVER['QUERY_STRING'])) {
- $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
- //Vevb.com
- }
- }
- 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 即可(在win下是不可能這樣命名的)
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的主機上安裝阿帕奇之后再用。
新聞熱點
疑難解答
圖片精選