Need a way to 301 redirect http://mysite.com/index.htm to http://www.mysite.com/index.htm

Discussion in 'Site Design, SEO, Google and Site Promotion' started by mluirette, Aug 23, 2006.

  1.  
  2. If you're running asp.net 2.0, you should be able to run both C# and VB.NET in the same web application.


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  3. Well Joel, I implemented the VB version of thecode you supplied above. Using VWD 2005 Express two errors were indicated. Here is the code as Icorrected it(correctly, I hope) and placed in my global.asax file:


    -----


    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)


    Dim redirectFrom As String = "localhost"


    Dim redirectTo As String = "www.localhost"





    If (String.Compare(Request.ServerVariables("SERVER_NAME"), redirectFrom, True) = 0) Then


    Response.Redirect(String.Format("http{0}://{1}{2}{3}", _


    IIf(Request.IsSecureConnection, "s", ""), _


    redirectTo, _


    IIf(Request.Url.Port <> 80, Request.Url.Port.ToString(), ""), _


    Request.Url.PathAndQuery))


    End If





    End Sub


    -----


    I changed the "== 0" to "= 0", and the "!= 80" to "<> 80".


    Unfortunately, when I open a new browser and enter either www.mydomain.com or mydomain.com, I am NOT rediredted to my prefered www.mydomain.com.


    Please help.
     
  4. Hello Joel -


    I did as you suggested immediately above, and then stopped and restarted the site in the Control Panel, to no avail. When I enter http://mysite.com, the site doesNOT redirect to http://www.mysite.com.


    By the way, once this code works, will typing in the mysite.com return a 301 error, when it redirects to www.mysite.com?


    Also,will using the code introduce any performance issues?
    I am eager to see this code work, and appreciate your assistance.
     


  5. Every request is now intercepted and tested, though this performace loss should not be measurable.


    The redirect will appear seemless from the viewers point of view.


    Please paste your whole global.asax (with vb or cs) so I can look at it.


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  6. Hello Joel -

    The entire Global.asax file appears between the dash lines below:

    ----------

    <%@ Application Language="VB" %>
    <script runat="server">
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application shutdown
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim redirectFrom As String = "localhost"
    Dim redirectTo As String = "www.localhost"

    If (String.Compare(Request.ServerVariables("SERVER_NAME"), redirectFrom, True) = 0) Then
    Response.Redirect(String.Format("http{0}://{1}{2}{3}", _
    IIf(Request.IsSecureConnection, "s", ""), _
    redirectTo, _
    IIf(Request.Url.Port <> 80, Request.Url.Port.ToString(), ""), _
    Request.Url.PathAndQuery))
    End If
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
    End Sub
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a new session is started
    End Sub
    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a session ends.
    ' Note: The Session_End event is raised only when the sessionstate mode
    ' is set to InProc in the Web.config file. If session mode is set to StateServer
    ' or SQLServer, the event is not raised.
    End Sub

    </script>

    ----------

    When googlebot visits (after this code is working) it needs to see a 301 header indicating that the file (mysite.com/somepage) has been permanently moved (to www.mysite.com/somepage). That is what SEO types are concernte about.

    Thanks in advance for your assistance.
     
  7. Hello Joel -

    No I had not changed out the localhost to mysite.comand www.localhost to www.mysite.com. When I did I got the following results:

    1) when I typed in "www.mysite.com" I was taken to "http://www.mysite.com". Good.

    2) when I typed in "www.mysite.com/somepage.aspx" that is what I saw. Good.

    3) when I typed in "mysite.com" I was taken to "http://mysite.com/" very briefly (long enough to read the URL) and then onto"www.mysite.com/default.aspx". Good and BAD.

    4) when Ityped in"mysite.com/somepage.aspx"I was immediatelytaken to"http://<a target="_blank" href="http://www.mysite.com/somepage.aspx">www.mysite.com/somepage.aspx</A>". Good.

    Theproblem remains at two levels:

    1) the whole purpose of this code is to avoid the "duplicate page" issue raised by Google (Google treates www.mysite.com and mysite.com as two different pages with identical content. It includes both pages in it's index. The one with the higher pagerank is generally the one that will get the better ranking. This introduces many difficulties for SEOs who are concerned about ethically getting the highest possible ranking in the search results for target search phrases.) When the googlebot visits "mysite.com" we want to return a 301 (page permanently moved) error code before the redirect. This is so Google will remove "mysite.com" pagesfrom their index - and all is well.

    2) the duplicate page issue cancome into playwhenthe default page within the root, or any subfolder, is sometimes referenced byby thefolder, not the specific default page. This is to avoidGoogle indexing both"www.mysite.com/" and "www.mysite.com/default.aspx" (or .htm or .html or .index, etc). In #3 above your code automatically redirects from "mysite.com" to "http://www.mysite.com/default.aspx". This introduces a potential "duplicate page" issue on the most important page ofmost web sites - the home page.

    The issue boils down to this:

    Is there a way the code can redirect from "mysite.com" to "http://www.mysite.com/" without including the reference to the specific default page?

    Can the redirection of default pages at thesubfolder level, be to the folder instead of including the specific default page of that folder?

    Can your code be made to return a 301 error before redirecting?

    Please advise. Thanks.

    PS: Did you know that "localhost" works just fine in the code used to send email via smtp? That is why I assumed that you meant for me to use "localhost" instead ofme domain name in this case.
     
  8. Hello Joel -


    Regarding my post immediately above, here is what is returned when I type "mysite.com" into a new browser window:


    HTTP/1.1 302 Found
    Connection: close
    Date: Tue, 27 Mar 2007 21:42:22 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Location: http://www.mysite.com/default.aspx
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Content-Length: 153

    I need your code to return"HTTP/1,1 301 Permanently Moved" instead. That way Google notes that the "mysite.com" no longer exists, and Google drops it from their index.

    Please help me.
     
  9. 302 and 301 should have the same effect in google. When google attempts to crawl the page, it will get a 302 and remove it from the index. Because there is no content on this page, it cannot be added to the search engines index.


    If you want to send a 301, you have to do it manually.


    Replace the Response.Redirect line with these 3 lines...


    Response.StatusCode = 301;
    Response.AddHeader(
    "Location",
    String.Format("http{0}://{1}{2}{3}",
    Request.IsSecureConnection ? "s" : "",
    redirectTo,
    Request.Url.Port != 80 ? Request.Url.Port.ToString() : "",
    Request.Url.PathAndQuery
    ));
    Response.End();


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     

Share This Page