2011-01-23 112 views
1

我有2個域名解析到我的網站www.mysite.com和www.mysitecommonmisspelling.com,再加上IP地址。我在Global.asax文件一些代碼,旨在強制所有用戶和爬蟲只使用主域名,就像這樣:ASP.NET - 客戶端可以禁用/忽略301重定向嗎?

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs) 

    'If the domain isn't the correct top level domain for this environment, 
    'we want to do a 301 redirect to tell the search engines that this is not the right domain to 
    'index. 
    Dim Host As String = Me.EnvironmentHost 
    Dim CurrentHost As String = Request.Url.DnsSafeHost 

    If CurrentHost.ToLower <> Host.ToLower Then 
     Dim Url As String = Request.Url.AbsoluteUri 

     'The AbsolutUri property returns the Default.aspx whether it was actually specified 
     'in the URL or not. Since our server is set up to respond to this as the default page, 
     'we can safely remove it from the URL if it actually exists. We don't want Default.aspx 
     'indexed anyway if possible. 
     Url = Url.Replace("Default.aspx", "") 
     Url = Url.Replace("default.aspx", "") 

     'Replace the current host with the environment host 
     Url = Url.Replace(CurrentHost, Host) 


     Response.Clear() 
     Response.StatusCode = 301 
     Response.Status = "301 Moved Permanently" 
     Response.AddHeader("Location", Url) 
     Response.End() 

    End If 

End Sub 

我檢查,谷歌還沒有收錄在www.mysitecommonmisspelling任何頁面。因此他們顯然尊重301.但是,前幾天我們在我們的網站上有一位客戶獲得免費送貨服務,因爲僅註冊www.mysite.com的授權碼失敗(換句話說,瀏覽器是能夠訪問www.mysitecommonmisspelling.com)。過去,由於用戶能夠使用IP地址訪問站點,因此我們在第三方託運組件方面遇到了問題。

在任何情況下,我們都不希望用戶能夠在沒有正確域的情況下訪問站點,因此SSL證書不會投訴。

我已經搜索了一種方法來禁用IE7中的301重定向,但似乎沒有辦法。那麼,我想知道的是,是否有任何瀏覽器可以禁用301,如果有的話,我可以採取什麼樣的解決方法來確保我網站上的所有瀏覽器都轉到主域www.mysite.com?

回答

0

這種類型的重定向確實屬於Web服務器,而不是應用程序。對於IIS7,有URL Rewrite Module

您可能需要設置IIS來託管兩個網站MySite和Misspelling,以便拼寫檢查從未真正觸及過您的真實網站。只需在該網站上設置重定向。

你可能想檢查Server Fault如何正確地做到這一點,因爲我不是IIS7專家。

對於瀏覽器忽略它的部分問題:當然,瀏覽器可以決定不遵循301重定向,但他們只會顯示一個空白頁。如果有人可以繞過它,那麼這意味着這條線:

If CurrentHost.ToLower <> Host.ToLower Then 

無法正常工作。我不知道瀏覽器是否可以在訪問不同的主機名時發送錯誤的主機名(儘管如果有疑問,我會責怪Proxies,因爲它們通常很奇怪)。

+0

謝謝。其實,我正在使用IIS6。我會考慮將其分成兩個不同的網站。我想到了另一個潛在的場景:Application_BeginRequest不會觸發嗎? – NightOwl888 2011-01-23 08:47:04