2015-07-13 50 views
0

我如何才能301重定向我的網址到non-www equivalent with trailing slash最後只有一個單301重定向(避免重定向鏈)?我正在使用ASP.net 4.5/C#/ Web Project,註冊路線爲RouteConfig.csnon-www with trailing slash 301在web.config中重定向(單一重定向)

一個選項是檢查每個頁面後面代碼中的URL並重建URL,但我更願意讓IIS使用重寫規則處理它。 (通過chrome,客戶端),有兩個301重定向,因爲在我的web.config中,我有兩個規則,一個用於更改爲小寫URL,第二個用於添加尾部斜槓。

enter image description here

也許有IIS中的選項,以防止重定向,直到所有的URL重寫內部跑去。我搜索了它,但還找不到解決方案。

回答

1

在web.config中(你需要安裝IIS URL重寫模塊)

<system.webServer> 
     <rewrite> 
      <rules> 
       <rule name="noslash" patternSyntax="ECMAScript" stopProcessing="true"> 
        <match url=".+[^\/]$" /> 
        <conditions logicalGrouping="MatchAny"> 
         <add input="{HTTP_HOST}" pattern="www.yourdomain.com" /> 
         <add input="{HTTP_HOST}" pattern="yourdomain.com" /> 
        </conditions> 
        <action type="Redirect" url="http://yourdomain.com/{R:0}/" /> 
       </rule> 
       <rule name="www" patternSyntax="ECMAScript" stopProcessing="true"> 
        <match url=".+\/$" /> 
        <conditions logicalGrouping="MatchAny"> 
         <add input="{HTTP_HOST}" pattern="www.yourdomain.com" /> 
        </conditions> 
        <action type="Redirect" url="http://yourdomain.com/{R:0}" /> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 

EDIT2:加在URL的結尾斜槓,如果它不存在

+0

感謝,但我認爲,這隻適用於www到非www,後面的斜槓不會被記入acount和單個重定向中。 –

+0

我編輯了重定向URL,現在它添加了尾部斜槓 – Andrey

+0

Yeh,但只有當url有www時纔會發生添加的斜槓。如果URL沒有www,它將不會追加尾部斜線。 –