【问题标题】:How to remove the www. prefix in ASP.NET MVC怎么去掉www。 ASP.NET MVC 中的前缀
【发布时间】:2011-06-20 10:19:59
【问题描述】:

如何删除 www.来自传入的请求?我需要设置 301 重定向还是只是重写路径?无论哪种方式,最好的方法是什么?

谢谢!

【问题讨论】:

  • 你为什么不添加指向www.domain.comdomain.com 的CNAME?
  • 在哪里你想删除它?您要在在您的应用程序中 删除它吗?或者您是否希望您的用户永远不会看到“www”。即使他们输入了地址?
  • @Mauricio 我想要的是即使他们输入 www 我想要删除它。
  • no-www.com 批准此消息。

标签: asp.net .net-4.0 url-rewriting asp.net-mvc-3


【解决方案1】:

我相信用 IIS 的 URL 重写模块来做这件事会更合适。

如果您有权访问 IIS 的管理工具,则在您网站设置的“IIS”部分中,有一个用于设置重写规则的 GUI。如果您从那里(在右栏菜单中)选择“添加规则...”,请在 SEO 部分选择“规范域名”规则,以几乎完全自动设置规则。

如果不是,重写规则在您的 web.config 中将如下所示:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="CanonicalHostNameRule1">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^yourdomain\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://yourdomain.com/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

【讨论】:

  • 我一定在这里遗漏了一些东西。如果我将它插入到我的 web.config 中,我会收到“HTTP 错误 500.19 - 内部服务器错误无法访问请求的页面,因为该页面的相关配置数据无效。”我删除了重写部分,事情又开始了。有什么我在这里想念的想法吗?
  • @Kjensen:确保您安装了 URL 重写扩展。你可以在这里得到它:iis.net/download/urlrewrite
【解决方案2】:

您可以处理Application.BeginRequest事件并检查Request.Host是否以www开头。
如果是,请调用 Response.RedirectPermanent,并传递一个带有请求路径和裸域的 URL。

你可以通过写来构造新的 URL

"yourdomain.com" + Request.Url.PathAndQuery

【讨论】:

    【解决方案3】:

    我在这里找到了一个很好的解决方案:http://nayyeri.net/remove-quotwwwquot-from-urls-in-asp-net

    public class RemoveWWWPrefixModule : IHttpModule
    {
        public void Dispose() { }
    
        private static Regex regex = new Regex("(http|https)://www\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            Uri url = application.Context.Request.Url;
            bool hasWWW = regex.IsMatch(url.ToString());
    
            if (hasWWW)
            {
                String newUrl = regex.Replace(url.ToString(),
                String.Format("{0}://", url.Scheme));
                application.Context.Response.RedirectPermanent(newUrl);
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以在 web.config 文件中使用以下重写规则。

      此重写规则将删除 WWW 并保留尾随 url 和启动协议。

          <rewrite>
             <rules>
                <clear/>
                   <rule name="Canonical host name" enabled="true">
                      <match url="(.*)"/>
                      <conditions trackAllCaptures="true">
                          <add input="{HTTP_HOST}" negate="false" pattern="^www\.(.+)$"/>
                          <add input="{CACHE_URL}" pattern="^(.+)://" />
                      </conditions>
                      <action type="Redirect" url="{C:2}://{C:1}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent"/>
                   </rule>
             </rules>
          </rewrite>
      

      【讨论】:

        【解决方案5】:

        这是更通用的配置,因为您可以在根 IIS 的 URL 重写中编写一次(不特定于某个应用程序池),它将自动应用于您的所有 IIS 网站,而不依赖于您的域名。

        【讨论】:

          【解决方案6】:

          更简单的解决方案是创建动作过滤器并用它装饰您的动作。

          public class RemovePrefix : ActionFilterAttribute
          {
              public override void OnActionExecuting(ActionExecutingContext filterContext)
              {
                          var url = filterContext.HttpContext.Request.Url.ToString().Replace("http://www.", "http://");
                          filterContext.Result = new RedirectResult(url);
          
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2015-05-15
            • 1970-01-01
            • 1970-01-01
            • 2014-08-05
            • 1970-01-01
            • 1970-01-01
            • 2021-11-03
            • 1970-01-01
            • 2018-09-13
            相关资源
            最近更新 更多