如何通过Web.config或C#将所有内容重定向到https:// www。该网站的版本?

时间:2011-02-15 16:33:36

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

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

例如:

这在Linux或cPanel托管上是小菜一碟。但是我不知道怎么在GoDaddy上做这个(我问过支持,但他们似乎什么都不知道......)。我想用IIS很容易,但是GoDaddy没有给你IIS访问权。

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

2 个答案:

答案 0 :(得分:5)

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

从ASP.NET发布永久重定向

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

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 Rewrite Module。检查支持。

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

<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>

通过第三方应用程序重写

许多网络托管公司都安装了第三方网址重写应用程序,例如ISAPI_Rewrite。我不知道GoDaddy是否有这个,你需要检查支持。但这可能是另一种选择。

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

您不必担心将用户重定向到www.版本,您可以让用户按照他们的意愿到达您的网站,但会在您的网页上添加标记,以告诉搜索引擎您的网址的规范形式。

要指定规范网址,只需在网页的<link>部分添加<head>元素即可。其工作方式如下 - 将以下标记添加到您希望搜索引擎考虑所有相同网址的网站中这些页面的<head>部分:

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

如果您在Stackoverflow.com上执行查看/来源,则会看到此<link>标记。这是确保您网站中重复网址(例如www.yoursite.com/foo.aspxyoursite.com/foo.aspx)被视为搜索引擎索引中相同条目的好方法。

快乐编程!

答案 1 :(得分:1)

感谢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);
}