近幾天公司里開發的項目有幾個運行在IIS7.5上,由于全站采用的是偽靜態,因此從網上找到兩兩種方法來實現。這兩種方法各有優勢:第一種比較靈活,只要把文件拷到根目錄下,即可直接顯示所有偽靜態頁面(適用于此偽靜態規則的所有項目,如ThinkPHP),無需更改代碼;第二種適合有子目錄時的偽靜態,比如一個網站下有多個子網站且都要使用偽靜態,那么就考慮使用第二種方法了,第一種會報錯誤。兩種方法,自己根據情況使用吧(當然,并不是適用所有項目,可以根據項目的偽靜態規則自行調整)。以下是代碼:
第一種方法:web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="OrgPage" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(.*)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
第二種方法:web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="規則 1" stopProcessing="true">
<match url="^includes/(.*)" />
<action type="Rewrite" url="includes\/{R:1}" />
</rule>
<rule name="規則 2" stopProcessing="true">
<match url="^(blog)/includes/(.*)" />
<action type="Rewrite" url="{R:1}/includes\/{R:2}" />
</rule>
<rule name="規則 3" stopProcessing="true">
<match url="^(blog)/(.*).html(.*)" />
<action type="Rewrite" url="{R:1}/index.php\/{R:2}.html{R:3}" />
</rule>
<rule name="規則 4" stopProcessing="true">
<match url="^(.*).html(.*)" />
<action type="Rewrite" url="index.php\/{R:1}.html{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
下面是補充:
IIS 7和IIS 7.5及以后的版本估計都會使用web.config來實現偽靜態規則,于是我們以前的偽靜態文件必須更改。網上找了一圈,還沒有發現比較全面的web.config偽靜態規則,于是我們這里整理一份,供初次使用的朋友參考。
實現普通頁面、帶一個數字參數頁面和帶兩個參數頁面的偽靜態!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > < system.webServer > < rewrite > < rules > < rule name = "Index" stopProcessing = "true" > < match url = "^index.html" /> < action type = "Rewrite" url = "index.php" /> </ rule > < rule name = "Rule1" stopProcessing = "true" > < match url = "^news_([0-9]+).html" /> < action type = "Rewrite" url = "news.php?nid={R:1}" /> </ rule > < rule name = "Rule2" stopProcessing = "true" > < match url = "news_list_([0-9]+)_([0-9]+).html" /> < action type = "Rewrite" url = "news_list.php?nid={R:1}&page={R:2}" /> </ rule > </ rules > </ rewrite > </ system.webServer > </ configuration > |
IIS 7.5通過web.config實現301重定向的方法,將不帶www的域名轉向到帶www的域名上!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > < system.webServer > < rewrite > < rules > < rule name = "Redirect" stopProcessing = "true" > < match url = ".*" /> < conditions > < add input = "{HTTP_HOST}" pattern = "^zzvips.com$" /> </ conditions > < action type = "Redirect" url = "http://www.ythuaji.com.cn/{R:0}" redirectType = "Permanent" /> </ rule > </ rules > </ rewrite > </ system.webServer > </ configuration > |
由于我們的網站使用了轉義字符,因此在實際使用的時候,大家不可以直接復制以上代碼。請復制粘貼到Dreamweaver等編輯器后,使用替換功能把雙引號全部替換為英文狀態下的雙引號,然后再修改rule標簽內的內容就可以了,跳轉的地方請更改為自己的網址即可。
需要注意的地方是以前httpd.ini和.htaccess支持網址中兩個參數用&符號鏈接,在web.config中是不支持的,需要將這個符號更改為&才能正常使用。由于我們目前只有一臺這種類型的服務器使用經驗,有可能存在不足,如有更多更全面的資料,歡迎交流學習!