We help IT Professionals succeed at work.

IIS6 and ASP.net 4 and 301 redirect

Don't you hate it when you do everything that a forum says to do and it still does not work?  My latest problem is this:  I have a website that I optimizing.  One of the things I need to do is to a 301 permanent redirect from the non-www url to the www url.  So http://mydomain.com redirects to http://www.mydomain.com.  The site is hosted in a shared environment running IIS6.  The site is an asp.net 4 site.  I set up a new website to handle the non-www url and create a permanent redirect to the www. site.  The problem is that when I type in http://mydomain.com, the redirect appends with “/eurl.axd/GUID”   I have followed the suggestions of many forums to resolve the issue and yet nothing works.  Some suggest making sure the site use different application pools, some suggest using the same.  Some suggest using asp.4 for one and asp.net2 for the other, while some suggest using the same version for both.  I also tried to add DWORD EnableExtensionlessUrls to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\ASP.NET\4.0.30319.0\ and still did not work.  Does anyone know how to solve this problem?

Thanks
Comment
Watch Question

/eurl.axd/GUID - This is a feature in asp.net 4.0 to append to extension less url.

Did you try creating site http://mydomain.com with 2.0 framework and redirecting to http://www.mydomain.com ?

As you said above create new website with framework 2.0 to handle the non-www url and create a permanent redirect to the www.

Author

Commented:
Yes I did. It did not work for me. I am willing to try it again just to make sure I didn't do it wrong the first time.
GlobaLevelProgrammer

Commented:
okay so you have been working on this issue for quite sometime with quite alot of resources. So just jumping in, I assume you already have two virtual directories in IIS is that correct?

Also what happens when you ping the URLS, what ip do you get?

Author

Commented:
Yes I have two virtual directories.  When I ping urls, I get response from each and the same ip address at each.
GlobaLevelProgrammer
Commented:
okay so you can do the redirect in two ways...preprocessing-at the web server level or the application itself....since you have been turning the wheels on this for awhile...and the boss whats it fixed now...you can redirect at the app level so in visual studio open the web app find the page that is set to the start page...go to the code behind/inline and add a method on page_load:

vb:
public sub page_load(object as sender, eventargs as e) handles me.load
reponse.redirect("http://www.mydomain.com")
exit sub
end sub


----so thats the quick fix in the meantime...IIS web server

in IIS, open virtual directory and left click open properties and and see where the directory is pointing and make sure its point to the correct folder where each web is contained..there can be two or one....depending what you want...you can point to the same www. folder...does this help?

Author

Commented:
Thanks so much.  I will do the redirect programatically today.  I was under the impression that doing it this way is not as good for SEO which is why I have avoided it so far.  And yes in IIS the directory does point to the correct folder.  Both virtual directories point to the www folder.
I found a solution.  Just in case anyone else is having the same problem the solution is to code the redirect in the global.asax file. Here is the code:

Thanks for eveyones help
Protected Sub Application_BeginRequest(ByVal sender As [Object], ByVal e As EventArgs)
        Dim server As String = Request.ServerVariables("SERVER_NAME")
        Dim url As String = Request.ServerVariables("URL")
        Dim querystring As String = Request.ServerVariables("QUERY_STRING")
        Dim fullurl As String = server & url

        Dim tail As String = String.Empty
        If Not String.IsNullOrEmpty(querystring) Then
            tail = "?" & querystring
        End If

        Dim patternwww As String = "www."

        If Not fullurl.Contains(patternwww) Then

            Dim targeturl As String = "http://" & patternwww & fullurl

            If Not String.IsNullOrEmpty(tail) Then
                targeturl &= tail
            End If

            ' Create the 301 Redirect
            Response.Clear()
            Response.Status = "301 Moved Permanently"
            Response.AddHeader("Location", targeturl)
            Response.[End]()
        End If

    End Sub

Open in new window

Author

Commented:
This solution works around the IIS 301 redirect problem in shared hosting environments and also is perfectly acceptable for SEO.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.