2012-01-11 61 views
3

我一直在關注http://learn.iis.net/page.aspx/806/seo-rule-templates/,這是一個在IIS7中創建搜索引擎友好URL的幾乎完美的指南。在IIS7中重寫URL

我雖然一個問題:

如果我創建一個規則來重寫www.domain.com/contact/我在web.config中得到:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true"> 
    <match url="^([^/]+)/?$" /> 
    <conditions> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    </conditions> 
    <action type="Rewrite" url="?p={R:1}" /> 
</rule> 

但後來我做不到www.domain.com/contact/send/

如果我創建一個規則來重寫www.domain.com/contact/send/我得到在web.config中:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true"> 
    <match url="^([^/]+)/([^/]+)/?$" /> 
    <conditions> 
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
    </conditions> 
    <action type="Rewrite" url="?p={R:1}&amp;a={R:2}" /> 
</rule> 

但我不能這樣做www.domain.com/contact/

兩者的規則去做,讓我看不到任何腳本,來自我的/scripts/,/css//images/文件夾的css或og圖像。

我該如何制定一條規則來匹配AND和NOT以匹配上面提到的3個文件夾?

+0

沒有人能回答我這個問題嗎? :/ – Behrens 2012-01-12 10:14:21

回答

3

你的規則可以是這樣的(我已經擴大,並評論它更容易理解和最終編輯):

^ 
    (
     (?!css|scripts|images) # The directories you don't want to match 
     [^/]+     # The matched directory 
    ) 
    (
     /
     (
      ([^/]+)    # Optional part 
      /?     # Optional trailing slash 
     )? 
    )? 
$ 

換算成以下幾點:

<match url="^((?!css|scripts|images)[^/]+)(/(([^/]+)/?)?)?$" /> 

重寫由於新正則表達式捕獲次數的變化,網址應該更新爲:?p={R:1}&amp;a={R:4}

+0

這是完美的..非常感謝! :) – Behrens 2012-01-18 16:01:01

+0

不客氣! – 2012-01-18 16:48:56