![](/img/trans.png)
[英]How to redirect http to https and www to non-www via web.config?
[英]non-www with trailing slash 301 redirect in web.config (Single Redirect)
如何僅通過單個301重定向 (避免重定向鏈)將301的 URL重定向到non-www equivalent with trailing slash
並在末尾添加non-www equivalent with trailing slash
? 我將ASP.net 4.5 / C#/ Web項目與在RouteConfig.cs
注冊的路由RouteConfig.cs
。
一種選擇是檢查每個頁面后面代碼中的URL並重建URL,但是我更喜歡讓IIS使用Rewrite規則來處理它。
從該圖像(通過chrome,客戶端)可以看到,有兩個301重定向,因為在我的web.config中,我有兩個規則,一個規則更改為小寫URL,第二個規則添加斜杠。
IIS中可能有一個選項可以阻止重定向,直到所有URL重寫都在內部運行為止。 我進行了搜索,但找不到解決方案。
在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:如果網址末尾不加斜杠
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.