我在GoDaddy上托管了一个ASP.NET网站。我需要将每个请求重定向(301)到https://www.example.com/whatever
例如:
这在Linux或cPanel托管上是小菜一碟。但是我不知道怎么在GoDaddy上做这个(我问过支持,但他们似乎什么都不知道......)。我想用IIS很容易,但是GoDaddy没有给你IIS访问权。
有没有办法通过Web.config或C#来实现?最好是web.config
答案 0 :(得分:5)
查看我的这篇文章,它提供了许多不同的选项来避免ASP.NET站点中的重复URL:Techniques for Preventing Duplicate URLs in Your Website。我已经总结了下面的各种技术,但请参阅文章以获得更深入的处理,以及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);
}
如果您的网站由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.aspx
和yoursite.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);
}