2011-09-07 51 views
2

我剛剛升級到VS2010/IIS 7.5/URL Rewrite 2.0。我想要做的事情非常簡單,但我真的很厭倦試圖讓自己獨立工作。URL重寫規則語法問題

我只是想乾淨網址,藉此http://example.com/abc-def.aspx變得http://example.com/abc-def/,有效地消除了.aspx擴展並添加斜線。

我已經做了,通過使用:

<rule name="Trim aspx for directory URLs"> 
    <match url="(.+)\.aspx$" /> 
    <action type="Redirect" redirectType="Permanent" url="{R:1}/" /> 
</rule> 

這工作得很好,並重定向如預期,但沒有拉起的頁面,所以我想我需要結合起來,與一個重寫規則,使它會將乾淨的URL解析到相應的.aspx頁面。

我試着用做:

<rule name="Add aspx extension back internally"> 
    <match url="^http://example\.com/(.+)/$" ignoreCase="true" /> 
    <conditions> 
     <add input="{URL}" matchType="IsDirectory" negate="true" /> 
     <add input="{URL}" pattern=".+/externals/.+" negate="true" /> 
    </conditions> 
    <action type="Rewrite" url="{R:1}.aspx" /> 
</rule> 

的重定向規則的作品,但它好像內部重寫規則不起作用,因爲該頁面不起來。我究竟做錯了什麼?

回答

1

不能確定是否有與URL的方式重寫2.0做到這兩點:

  1. 上要求刪除擴展名爲.aspx,並添加結尾斜線/在未來
  2. 內部添加的.aspx擴展回來,正確的頁面負載,而不是得到一個404

我決定做的是改變無處不在源點到.aspx擴展名的URL,這樣永遠不應該有一個外REQ最後用一個以.aspx結尾的URL。

,讓我只需要:

<rule name="Add aspx extension back internally" stopProcessing="true"> 
    <match url="(.+)/$" /> 
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Rewrite" url="{R:1}.aspx" /> 
</rule> 

<rule name="Add trailing slash" stopProcessing="false"> 
    <match url="(.*[^/])$" /> 
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
     <add input="{URL}" pattern="favicon\.ico" ignoreCase="true" negate="true" /> 
     <add input="{URL}" pattern="\.axd" ignoreCase="true" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="{R:1}/" /> 
</rule>