【问题标题】:How to redirect everything via Web.config or C# to https://www. version of the site?如何通过 Web.config 或 C# 将所有内容重定向到 https://www。网站的版本?
【发布时间】:2011-06-27 17:56:55
【问题描述】:

我有一个托管在 GoDaddy 上的 ASP.NET 网站。我需要将每个请求重定向(301)到https://www.example.com/whatever

例如:

这是 Linux 或 cPanel 主机上的小菜一碟。但我不知道如何在 GoDaddy 上执行此操作(我要求支持,但他们似乎什么都不知道......)。我想用 IIS 很容易做到,但 GoDaddy 不给你 IIS 访问权限。

那么有没有办法通过 Web.config 或 C# 来实现呢?最好是web.config

【问题讨论】:

    标签: asp.net redirect http-status-code-301


    【解决方案1】:

    查看我的这篇文章,它提供了许多不同的选项来避免在您的 ASP.NET 站点中出现重复的 URL:Techniques for Preventing Duplicate URLs in Your Website。我总结了下面的各种技术,但请参阅文章以获得更深入的处理,以及示例 ASP.NET 网站演示。

    从 ASP.NET 发出永久重定向

    您可以在 Global.asax 文件的 Application_BeginRequest 事件处理程序中检查每个请求的传入 URL。如果 URL 缺少前导 www.,您可以附加它并永久重定向到新 URL。

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
       if (Request.Url.Authority.StartsWith("www"))
          return;
    
       var url = string.Format("{0}://www.{1}{2}",
                   Request.Url.Scheme,
                   Request.Url.Authority,
                   Request.Url.PathAndQuery);
    
       Response.RedirectPermanent(url, true);
    } 
    

    使用 IIS 7 的 URL 重写模块将 URL 重写为规范形式

    如果您的网站由 IIS7 托管并且安装了 Microsoft 的 URL Rewrite Module,那么您可以在 Web.config 文件中添加重写规则。我没有使用 GoDaddy 托管的任何网站,所以我不知道他们是否使用 IIS7(我想他们会这样做)或者是否安装了 URL 重写模块。请与支持人员联系。

    假设满足这些先决条件,您将使用以下配置:

    <configuration>
       ...
    
       <system.webServer>
          <rewrite>
             <rules>
                <rule name="Canonical Host Name" stopProcessing="true">
                   <match url="(.*)" />
    
                   <conditions>
                      <add input="{HTTP_HOST}" pattern="^yoursite\.com$" />
                   </conditions>
    
                   <action type="Redirect" url="http://www.yoursite.com/{R:1}" redirectType="Permanent" />
                </rule>
             </rules>
          </rewrite>
       </system.webServer>
    </configuration>
    

    通过第三方应用程序重写

    许多网络托管公司都安装了第三方 URL 重写应用程序,例如 ISAPI_Rewrite。我不知道 GoDaddy 是否有此功能,您需要与支持人员联系。但这可能是另一种选择。

    在标记中告诉搜索引擎蜘蛛您的规范形式

    不必担心将用户重定向到www. 版本,您可以让用户随心所欲地到达您的网站,但在您的页面上添加标记以告知搜索引擎您的 URL 的规范形式。

    要指定规范 URL,只需在网页的 &lt;head&gt; 部分添加一个 &lt;link&gt; 元素。其工作方式如下 - 将以下标记添加到您网站中您希望搜索引擎考虑所有相同 URL 的那些页面的 &lt;head&gt; 部分:

    <link rel="canonical" href="canonical_url" /> 
    

    如果您在 Stackoverflow.com 上查看/源代码,您会看到这个 &lt;link&gt; 标记。这是确保您网站中重复的网址(例如,www.yoursite.com/foo.aspxyoursite.com/foo.aspx)在搜索引擎索引中被视为相同条目的好方法。

    编程愉快!

    【讨论】:

    • 谢谢,这正是我需要的。我会询问 GoDaddy 是否支持 URL 重写,如果不支持,我将使用 ASP.NET 实现。
    • 它运行良好,除了当我去 www.mysite.com 时,它似乎不会触发重定向,因为它会转到 http 而不是 https(正如我在 Redirect 元素中定义的那样) .也许我需要编辑条件?有什么帮助吗?谢谢
    • @emzero:也许你可以用你正在使用的代码发布一个新问题,并更详细地描述问题?'
    • 我发布了一个新的:stackoverflow.com/questions/5017632/…
    【解决方案2】:

    感谢 Scott 的出色回答!这很有帮助。

    这是 Application_BeginRequest() 代码的一个 mod,它 1) 使用序号字符串比较来提高速度,2) 适应 localhost 和 3) 突出显示不对称的副作用。

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        /**
         *  Note:
         *    Since Url.Authority always returns an all lowercase string, we can use 
         *    StringComparison.Ordinal (instead of OrdinalIgnoreCase) for www and 
         *    localhost checks.
         *  
         *  Ordinal rationale:
         *    "Use comparisons with StringComparison.Ordinal or 
         *    StringComparison.OrdinalIgnoreCase for better performance"
         *    see http://msdn.microsoft.com/en-us/library/dd465121.aspx#choosing_a_stringcomparison_member_for_your_method_call
         */
        if (Request.Url.Authority.StartsWith("www", StringComparison.Ordinal))
            return;
    
        /**
         * Avoid redirection on localhost.
         */
        if (Request.Url.Authority.StartsWith("localhost", StringComparison.Ordinal))
            return;
    
        /**
         * Because Uri.Authority is always lowercase, this code has the side-effect of 
         * enforcing a lowercase authority (e.g., http://NoisyKeys.com/About is 
         * converted to http://www.noisykeys.com/About).  This is asymmetric with 
         * previous conditionals.  To attain symmetry, we probably want to use 
         * Request.Url.OriginalString instead of Request.Url.Authority.
         */
        var url = string.Format("{0}://www.{1}{2}", 
            Request.Url.Scheme,
            Request.Url.Authority,
            Request.Url.PathAndQuery);
    
        Response.RedirectPermanent(url, true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-01
      • 2015-05-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2015-04-17
      • 1970-01-01
      相关资源
      最近更新 更多