如何使用"www"URL重定向到没有"www"URL,反之亦然?

Pra*_*ant 15 c# asp.net redirect canonical-name

我正在使用ASP.NET 2.0 C#.我想用"www"将我对web应用的所有请求重定向到没有"www"

www.example.com到example.com

要么

example.com到www.example.com

Stackoverflow.com已经这样做了,我知道PHP(.htaccess)文件中有预制机制.但是如何在asp.net中做到这一点?

谢谢

shs*_*mer 27

有关于此的Stackoverflow博客文章.

http://blog.stackoverflow.com/2008/06/dropping-the-www-prefix/

引用杰夫:

这是从所有传入的URL中删除WWW前缀的IIS7规则.将此XML片段剪切并粘贴到您的web.config文件下

<system.webServer> / <rewrite> / <rules>

<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
    redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)

或者,如果您更喜欢使用www前缀,也可以这样做:

<rule name="Add WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}"
    redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)

  • 也许我错过了一些东西,但你接受的答案要求你修改web.config.我的回答还要求你修改web.config. (2认同)

Zha*_*uid 6

当我无法修改IIS设置时,我已经使用了以下解决方案.

在ApplicationMeginRequest中的HTTPModule(可能是最干净的)或global.asax.cs中,或者在某些BasePage类型事件中,例如OnInit,我使用我希望使用的已知字符串对请求的URL执行检查:

public class SeoUrls : IHttpModule
{
  #region IHttpModule Members

  public void Init(HttpApplication context)
  {
      context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
  }

  public void Dispose()
  {
  }

  #endregion

  private void OnPreRequestHandlerExecute(object sender, EventArgs e)
  {
    HttpContext ctx = ((HttpApplication) sender).Context;
    IHttpHandler handler = ctx.Handler;

    // Only worry about redirecting pages at this point
    // static files might be coming from a different domain
    if (handler is Page)
    {
      if (Ctx.Request.Url.Host != WebConfigurationManager.AppSettings["FullHost"])
      {
        UriBuilder uri = new UriBuilder(ctx.Request.Url);

        uri.Host = WebConfigurationManager.AppSettings["FullHost"];

        // Perform a permanent redirect - I've generally implemented this as an 
        // extension method so I can use Response.PermanentRedirect(uri)
        // but expanded here for obviousness:
        response.AddHeader("Location", uri);
        response.StatusCode = 301;
        response.StatusDescription = "Moved Permanently";
        response.End();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在web.config中注册该类:

<httpModules>
  [...]
  <add type="[Namespace.]SeoUrls, [AssemblyName], [Version=x.x.x.x, Culture=neutral, PublicKeyToken=933d439bb833333a]" name="SeoUrls"/>
</httpModules>
Run Code Online (Sandbox Code Playgroud)

这种方法对我们很有效.


Pet*_*r J 5

接受的答案适用于单个URL或只是少数几个,但我的应用程序提供数百个域名(手动输入的URL太多).

这是我的IIS7 URL重写模块规则(此处的操作类型实际上是301重定向,而不是"重写").效果很好:

<rule name="Add WWW prefix" >
  <match url="(.*)" ignoreCase="true" />
  <conditions>
   <add input="{HTTP_HOST}" negate="true" pattern="^www\.(.+)$" />
  </conditions>
  <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" 
       appendQueryString="true" redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)