【问题标题】:ASP.net MVC site : Redirect all "non WWW" request to WWWASP.net MVC 站点:将所有“非 WWW”请求重定向到 WWW
【发布时间】:2013-04-03 20:03:39
【问题描述】:

最近我将一个 ASP.net 站点迁移到 ASP.net MVC 站点。之前有两个主机头,一个是 mydomain.com,另一个是 www.mydomain.com。我的 SEO 说你应该只使用一个 URL“www.domain.com”来获得 SEO 优势。

我正在寻找一个选项来执行 301 永久重定向所有 mydomain.com 请求到 www.mydomain.com。

该网站托管在 IIS6 中,并使用 ASP.net MVC 4 开发。

【问题讨论】:

    标签: asp.net asp.net-mvc routing seo iis-6


    【解决方案1】:

    您可以从您的 web.config 文件中执行此操作

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

    【讨论】:

    • 这是一个很好的答案,@Tommy;看不到一行 c#。
    • 您必须使用 IIS7 或更高版本才能使用 URL 重写模块。如果您至少有 IIS7,这是要走的路,但它不适用于 IIS6。
    • @johntrepreneur - 这是使用 IIS URL 重写模块,仅来自应用程序的配置文件。如果您有 IIS 7 并且可以使用此方法,那么通过 IIS 管理器控制台或使用 web.config 应该以相同的方式运行。如果您使用 .NET 路由功能,则必须将请求传递到 .NET 管道,这可能会导致性能轻微下降。我只会在无法使用重写模块恕我直言的情况下使用 .NET 路由。
    • @Tommy 信不信由你,在过去 2 年中必须引入一些政策,因为我在保存我的更改时收到错误,我不能再在答案中使用 mydomain.com 了:D 见@ 987654321@.
    • @MichalHosala - 它也得到了合理政策的大力支持。 +1 用于挖掘它,我不知道。酷!
    【解决方案2】:

    (需要 IIS 7 或更高版本)

    来自http://www.codeproject.com/Articles/87759/Redirecting-to-WWW-on-ASP-NET-and-IIS

    (与上述方案类似,但不需要您添加自己的域名。)

    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <clear />
                    <rule name="WWW Rewrite" enabled="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" negate="true"
                                pattern="^www\.([.a-zA-Z0-9]+)$" />
                        </conditions>
                        <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
                            appendQueryString="true" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    请注意,您很可能会在标签下方看到波浪线,并显示标签无效的消息。我收到了这条消息,但事实上,它工作得很好。

    如果您希望智能感知正常工作,可以在此处尝试此更新...

    http://ruslany.net/2009/08/visual-studio-xml-intellisense-for-url-rewrite-1-1/

    有关 httpRedirect 的更多信息可以在这里找到...

    http://www.iis.net/configreference/system.webserver/httpredirect

    【讨论】:

      【解决方案3】:

      很遗憾,URL 重写模块不适用于 IIS6(仅限 IIS7 或更高版本)。您是否考虑过创建自己的 HttpModule,类似这样的东西?

      IIS 6 how to redirect from http://example.com/* to http://www.example.com/*

      或者您可能会使用像以下之一这样的第 3 方解决方案:

      http://iirf.codeplex.com/

      http://www.urlrewriting.net/149/en/home.html

      http://www.isapirewrite.com/

      http://urlrewriter.net/

      【讨论】:

        【解决方案4】:

        您可以在 IIS 中使用 config 或 Url Rewriter,但我发现的最佳方法是将一些代码放入 Application_BeginRequest() 中的 global.asax.cs 中,如下所示:

        var HOST = "www.mydomain.com";
        
        if ( !Request.ServerVariables[ "HTTP_HOST" ].Equals(
          HOST,
          StringComparison.InvariantCultureIgnoreCase )
        )
        {
          Response.RedirectPermanent(
            ( HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://" )
            + HOST
            + HttpContext.Current.Request.RawUrl );
        }
        

        因为您是在代码中执行此操作,所以您可以根据每个请求拥有所需的任何逻辑。

        【讨论】:

        • 我在配置中的所有方面都尝试了 规则,但在 IIS 7 中没有成功。但这个解决方案对我有用。非常感谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 2015-09-30
        • 2022-01-09
        • 2012-04-18
        • 2015-04-20
        • 2011-06-29
        • 2015-02-25
        相关资源
        最近更新 更多