如何将asp.net默认子域设置为www?

如何将asp.net默认子域设置为www?,asp.net,Asp.net,如果我有一个网站myws.com,如果我请求myws.com或www.myws.com,我将看到相同的网站 我想要的是在请求“myws.com”时被重定向到“www.myws.com” 谢谢有很多方法可以实现www子域的规范重定向,如果您使用ASP.NET 4.0,您可以这样做: if (!Request.Url.Host.ToUpper().Contains("WWW")) { Response.RedirectPermanent("http://www.myws.com/" + Requ

如果我有一个网站myws.com,如果我请求myws.com或www.myws.com,我将看到相同的网站

我想要的是在请求“myws.com”时被重定向到“www.myws.com”


谢谢

有很多方法可以实现www子域的规范重定向,如果您使用ASP.NET 4.0,您可以这样做:

if (!Request.Url.Host.ToUpper().Contains("WWW"))
{
  Response.RedirectPermanent("http://www.myws.com/" + Request.Url.PathAndQuery, true);
}
您还可以使用,安装后将其添加到web.config文件:

<rewrite>
  <rules>
    <rule name="WWW Canonical Redirect" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^myws.com$" />
      </conditions>
      <action type="Redirect" url="http://www.myws.com/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

如果您有权访问IIS,您可以使用URL重写将流量从myws.com永久重定向到www.myws.com。遵循本教程:进行URL重写

如果您无权访问IIS,则可以将以下代码添加到应用程序_BeginRequest事件中的global.asax.vb中:

If Request.Url.Authority.ToString <> "www.myws.com" Then
    Response.RedirectPermanent(Request.Url.Scheme.ToString() & "://www.myws.com" & Request.Url.AbsolutePath.ToString())
End If
如果Request.Url.Authority.ToString“www.myws.com”,则
Response.RedirectPermanent(Request.Url.Scheme.ToString()&)://www.myws.com“&Request.Url.AbsolutePath.ToString())
如果结束