2014-03-19 101 views
0

我想重定向非www网站到www,但是当我在我的web.config中添加重定向规则或在我的Global.asax中显示该页面不是正确重定向。我试图这些事情在同一时间一个:在web.config中重定向非www到www结果在页面没有正确重定向

<rewrite> 
     <rules> 
     <rule name="CanonicalHostNameRule" enabled="true"> 
      <match url="(.*)" /> 
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
      <add input="{HTTP_HOST}" pattern="testdomain\.com$" /> 
      <add input="{HTTP_HOST}" pattern="^www\.testdomain\.com$" negate="true" /> 
      </conditions> 
      <action type="Redirect" url="http://www.testdomain.com/{R:1}" /> 
     </rule> 
     </rules> 
    </rewrite> 

我也内Application_BeginRequest 注意试过这种从Global.asax中的重定向规则。

if(HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://testdomain.com")) 
{ 
    HttpContext.Current.Response.Status = "301 Moved Permanently"; 
    HttpContext.Current.Response.AddHeader("Location", 
    Request.Url.ToString().ToLower().Replace("http://testdomain.com", "http://www.testdomain.com")); 
} 

我的Default.aspx和Default.aspx.cs页面没有代码吧.. 任何想法的家伙在那里实际的问题可能是。

回答

0

一次添加该代码的代码下<system.webServer>部分

<rewrite> 
<rule name="Redirect to www"> 
    <match url=".*" /> 
    <conditions logicalGrouping="MatchAny"> 
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/> 
</rule> 
</rewrite> 
+0

是我使用的一件事。我首先尝试了web.config ruie。然后我尝试了global.asax – dnts2012