我正在使用ASP.NET 4.我想做一個301重定向從http://sitename.com到http://www.sitename.com。從http://sitename.com重定向到http://www.sitename.com
什麼是最好的方式(和可選的方式)來做到這一點?
我的網站也在索引ip地址。我怎樣才能阻止這一點。
我正在使用ASP.NET 4.我想做一個301重定向從http://sitename.com到http://www.sitename.com。從http://sitename.com重定向到http://www.sitename.com
什麼是最好的方式(和可選的方式)來做到這一點?
我的網站也在索引ip地址。我怎樣才能阻止這一點。
你可以做的Page_Load
一個簡單的檢查:
protected void Page_Load(object sender, EventArgs e)
{
string serverName = HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);
string filePath = Request.FilePath;
if (!serverName.ToLower().StartsWith("www."))
serverName = "www." + serverName;
Response.Redirect("http://" + serverName + filePath);
}
或者你可以添加以下到htaccess文件:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
更換domain.com
和http://www.domain.com
與您的域名。
我會做301重定向在IIS級別,請參閱本博客文章:SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7
要停止的IP地址,configure the "site" in IIS to have a host name被索引的網站,不留空白。
我對IIS沒有太多控制權。你能否建議我一個替代方案 – 2011-05-11 15:27:11
如果你使用的是IIS7,並且託管服務提供商給你正確的權限,你可以在你的Web.config文件中的system.webServer部分配置它。 – tjrobinson 2011-05-11 15:33:21
是的,但url重寫模塊需要安裝在服務器上,它看起來不是。這就是爲什麼我需要替代品 – 2011-05-11 15:58:09
根據您的DNS提供商,他們可能會設置重定向,通常稱爲web forwards,web alias或web重定向。只要確保它是一個301不是一個框架。
<rule name="Redirect to www">
<match url="(.*)" />
<conditions>
<add input="{SERVER_PORT}" pattern="443" negate="true" />
</conditions>
<action type="Redirect" url="https://www.serdardemir.net/{R:1}" />
</rule>
您可以使用URL重寫添加此規則,以規則標籤
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
此代碼工作我。將其添加到web.config文件的規則部分
Response.Redirect發出302重定向,而不是301重定向。 .htaccess選項只適用於Apache,而不適用於IIS。 – tjrobinson 2011-05-11 14:37:16
你當然是對的......教我閱讀OP的想法。 – anothershrubery 2011-05-11 14:54:38