Asp.net 如何通过Web.config或C#将所有内容重定向到https://www. 网站的版本?
我有一个ASP.NET网站托管在GoDaddy上。我需要将(301)每个请求重定向到 例如:Asp.net 如何通过Web.config或C#将所有内容重定向到https://www. 网站的版本?,asp.net,redirect,http-status-code-301,Asp.net,Redirect,Http Status Code 301,我有一个ASP.NET网站托管在GoDaddy上。我需要将(301)每个请求重定向到 例如: 将被重定向到 将被重定向到 将被重定向到 这对于Linux或cPanel主机来说是小菜一碟。但我不知道如何在GoDaddy上做到这一点(我请求支持,但他们似乎什么都不知道…)。我想使用IIS很容易,但GoDaddy不允许您访问IIS 那么,有没有办法通过Web.config或C#实现这一点呢?最好是web.config查看我的这篇文章,它提供了许多不同的选项来避免ASP.NET站点中重复的URL:。
- 将被重定向到
- 将被重定向到
- 将被重定向到
那么,有没有办法通过Web.config或C#实现这一点呢?最好是web.config查看我的这篇文章,它提供了许多不同的选项来避免ASP.NET站点中重复的URL:。我在下面总结了各种技术,但请参阅本文以获得更深入的讨论,以及ASP.NET网站演示示例 从ASP.NET发出永久重定向 您可以在
Global.asax
文件的应用程序\u 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);
}
使用IIS7的URL重写模块将URL重写为规范形式
如果您的网站由IIS7托管,并且安装了Microsoft,则可以在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>
如果您在Stackoverflow.com上查看/Source,您将看到此
标记。这是一个很好的方法,可以确保站点中的重复URL(例如,www.yoursite.com/foo.aspx
和yoursite.com/foo.aspx
在搜索引擎索引中被视为相同的条目)
快乐编程 谢谢斯科特的精彩回答!这很有帮助 下面是对应用程序_BeginRequest()代码的一个修改,该代码1)使用顺序字符串比较来提高速度,2)适应本地主机,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);
}
谢谢,这正是我需要的。我会问GoDaddy他们是否支持URL重写,如果不支持,我会使用ASP.NET实现。它运行良好,但当我访问www.mysite.com时,它似乎不会触发重定向,因为它将转到http而不是https(正如我在Redirect元素中定义的那样)。也许我需要编辑条件?有什么帮助吗?Thanks@emzero:也许你可以发布一个新问题,其中包含你正在使用的代码和问题的更详细描述?“我发布了一个新问题:
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);
}