2011-05-11 82 views

回答

0

你可以做的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.comhttp://www.domain.com與您的域名。

+0

Response.Redirect發出302重定向,而不是301重定向。 .htaccess選項只適用於Apache,而不適用於IIS。 – tjrobinson 2011-05-11 14:37:16

+0

你當然是對的......教我閱讀OP的想法。 – anothershrubery 2011-05-11 14:54:38

0

我會做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被索引的網站,不留空白。

+0

我對IIS沒有太多控制權。你能否建議我一個替代方案 – 2011-05-11 15:27:11

+0

如果你使用的是IIS7,並且託管服務提供商給你正確的權限,你可以在你的Web.config文件中的system.webServer部分配置它。 – tjrobinson 2011-05-11 15:33:21

+0

是的,但url重寫模塊需要安裝在服務器上,它看起來不是。這就是爲什麼我需要替代品 – 2011-05-11 15:58:09

0
<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重寫添加此規則,以規則標籤

0
<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文件的規則部分