Solution for ASP.NET routing on GoDaddy shared hosting

Panning for Goldphoto © 2009 Caitlin Childs | more info (via: Wylio)I was doing a bit of research to try and find some nuggets of information on URL rewriting, especially with regard to GoDaddy’s shared hosting (I run this website (and others) on GoDaddy). I found this article in Google’s cache but the original site (codebeater.com) has gone away (I presume the domain wasn’t renewed). The article was by CodeBeater’s admin.

I didn’t want it to disappear into the ether as I can certainly see me using it in the future. So I copied it here verbatim, just cleaning up some look-n-feel.

[UPDATE 21-Feb-2011: Jeff from the CodeBeater site has revived his blog and has republished this post there.]

[LATER UPDATE 24-Nov-2012: the CodeBeater site has disappeared again.]

Solution for ASP.NET MVC Routing issue on Godaddy shared hosting

This is actually not an issue related to GoDaddy shared hosting at all but an issue with hosting an ASP.NET MVC site in a virtual directory. When you use the shared hosting provided by GoDaddy you get a root folder and limitless subfolders, each of which can be its own domain, by way of virtual directory. Unfortunately, MVC’s routing engine produces URLs that will include the virtual directory name appended to the domain name.

For example, let’s say you have a domain named http://www.example.com and your folder/virtual directory name is /File. If you take the MVC template project without making any modifications and upload it to your folder and then go to your url everything will look fine. You will notice the ‘Home’ and ‘About’ tabs at the top right of the page. When you click on the ‘About’ tab, since it is routed to the Home controller’s About action, you would rightly expect the URL to be www.example.com/Home/About. What you will see, though, is that the URL generated by the ActionLink method includes the name of the virtual directory. Therefore, the URL will be www.example.com/File/Home/About.

If this problem doesn’t bother you then kudos to you. It bothers me and I was really hoping to find a resolution that involved configuration, rather than writing a bunch of custom code for something I felt should work ‘out of the box’. After posting the question here and scouring various forums I found many others having the same problem but not one solution.

Luckily for me, I have a couple of friends and former coworkers that now work at GoDaddy who jumped on this issue when I posted it to their forums. I want to first say thanks to both of them for taking the time to dive into this and finding a configuration-based solution that works perfectly. You know who you are!

So now for the solution, and I probably should have mentioned this at the beginning but hopefully you’re using IIS7 with the URL rewriting module installed (it is installed by default when you use IIS7 on GoDaddy).

Simply add the following into your web.config’s system.webServer element:

<rewrite>
  <rules>
    <rule name="Remove Virtual Directory">
      <match url=".*" />
      <action type="Rewrite" url="{R:0}" />
    </rule>
  </rules>
</rewrite>

All this does is “rewrite” the URL with itself. This causes URL Rewrite to add the original URL (the one with no folder name) to a ServerVariable which is used by ASP.NET MVC to generate other URLs.

Again, this only works when you’re using IIS7 with the url rewriting module installed.

Good luck!

Album cover for Safe Trip HomeNow playing:
Dido - Northern Skies (Remix)
(from Safe Trip Home)


Loading similar posts...   Loading links to posts on similar topics...

20 Responses

 avatar
#1 Steve said...
03-Jan-11 4:52 PM

I added this to my web.config, but I'm still getting the folder added on postback. Like when I get redirected to a login page or submit an update to the site through my administration area.

 avatar
#2 Eric said...
09-Jan-11 12:50 PM

Thanks, Julian. I've been spinning my wheels trying to get that to work without getting my hands dirty tweaking the MVC source code. I dropped in your rewrite rule, and it worked perfectly. Very glad that I can leave my action links as-is.

Cheers!

Eric

 avatar
#3 Leonardo Lima said...
30-Jan-11 2:32 PM

I´ve the same problem, but I´m using Windows 2003 Server and IIS 6, anyone can help to solve this issue ?

 avatar
#4 Greg said...
06-Feb-11 9:39 PM

I am having the same problem with my godaddy hosting with asp.net mvc 3 repeating the virtual directory path in the url. But the rewrite rule did not solve the problem. Is this rewrite rule still working for you? I noticed your blog url is repeating the folder name "blog" twice.

julian m bucknall avatar
#5 julian m bucknall said...
07-Feb-11 7:27 PM

All: Just a quick comment to say I have not implemented this myself on this blog. The main reason is that I'm still running IIS6 on my hosting; I haven't upgraded to IIS7 yet. Why? Dunno really, apart from the fact it'll involve downtime and I have to ask support for the upgrade (if I recollect the steps correctly). So, as I said above, I was just making sure this particular article didn't disappear along with the blog that spawned it.

Another reason is that I'm playing around with ASP.NET MVC3, Razor, and Orchard with a view to migrating the content on this blog to something new. But I want to make sure I don't lose anything by doing so.

Cheers, Julian

 avatar
#6 CodeBeater said...
20-Feb-11 12:25 PM

Julian,

Thanks for keeping this post alive. CodeBeater disappeared because I just got sick of trying to work with things on Linux (it was a Wordpress blog). I didn't save anything before I killed it but it's back again and I've re-added this post with a small update that talks about something that still doesn't work after this fix.

Thanks again

 avatar
#7 CodeBeater said...
20-Feb-11 12:26 PM

Forgot to include a link to the post: www.codebeater.com/.../Solution-for-AS

julian m bucknall avatar
#8 julian m bucknall said...
21-Feb-11 8:36 AM

CodeBeater (aka, Jeff): Glad to hear you've brought the site back up and glad that I was able to help kick it off a little by saving this post. Good luck!

Cheers, Julian

 avatar
#9 stasevich said...
21-Feb-11 2:36 PM

works like a charm, spent 2+days looking for solution, trying different thing, got no help from GODADsters...

where can i read more on this?

 avatar
#10 stasevich said...
21-Feb-11 2:46 PM

works great!

 avatar
#11 chris patterson said...
24-Feb-11 1:06 PM

It looks like this nifty little rewrite rule works because of this function examining the HTTPXORIGINAL_URL server var in this function System.Web.Mvc.PathHelpers:

private static string GenerateClientUrlInternal(HttpContextBase httpContext, string contentPath)
{
  if (string.IsNullOrEmpty(contentPath))
  {
    return contentPath;
    }

  if (contentPath[0] == "~")
  { 
    string virtualPath = VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);
    string str2 = httpContext.Response.ApplyAppPathModifier(virtualPath);
    return GenerateClientUrlInternal(httpContext, str2);
  }

    NameValueCollection serverVariables = httpContext.Request.ServerVariables;
  if ((serverVariables == null) || (serverVariables["HTTP_X_ORIGINAL_URL"] == null))
  {
    return contentPath;
  }

  string relativePath = MakeRelative(httpContext.Request.Path, contentPath);

  return MakeAbsolute(httpContext.Request.RawUrl, relativePath);
}
 avatar
#12 Ian cullen said...
30-May-11 8:57 AM

adding this to my config file helped me

<modules runallmanagedmodulesforallrequests="true"></modules>

or

<modules runallmanagedmodulesforallrequests="true" />
 avatar
#13 Kashmir said...
01-Oct-11 3:15 AM

The above said solution is not working for asp.net 4.0.

 avatar
#14 Bikram said...
31-Mar-12 7:02 AM

But Can any one help me how to route url without using mvc and for every file i want to write one url algorithm in web.config or elsewhere ...so that my file can come like www.example.com/about instead of www.example.com/about.aspx..

plz plz frnd help me

 avatar
#15 Markus said...
05-Aug-12 7:16 PM

This doesn't appear to be working any more. Tried it on my go daddy hosting. IIS7.0 ASP.NET 4.0 MVC 3 app. still getting mysite.com/myfolder/Student instead of subsite.mysite.com/Student (which actually does work by the way), but all of the URL.Content and HTML.ActionLink references are using the full path.

 avatar
#16 Ashoka said...
21-Aug-12 12:20 AM

This is not working now.. I have posted this issue in codeplex blogengine forum too here > blogengine.codeplex.com/.../392425

 avatar
#17 Varun Sharma said...
16-Aug-13 10:53 AM

Hi there,

thanks for the post. I hosted purely an asp.net 4.0 web site and facing the same issue.

I have a domain DomainName.com and hosted the site to HVAC.DomainName.com where HVAC is a sub domain. THe site is not able to find the pages

all the route entries has been added like this:

routes.MapPageRoute("MaterialDetail", "Materials/Detail/{MaterialID}", "~/Materials/Detail.aspx");
routes.MapPageRoute("ClientDetail", "Clients/Detail/{ClientID}", "~/Clients/Detail.aspx");

but not working.

what to do.

THis is not mvc but pure asp.net

 avatar
#18 Paulo Gonçalves said...
17-Jun-15 5:07 AM

Varun Sharma, I had the same problem with routing in asp webforms in godaddy shared host, the problem has to due with FriendlyUrlSettings with AutoRedirectMode = RedirectMode.Permanent. Commented this, that came with c# project template and now routing works.

 avatar
#19 Arun Antony said...
21-Jan-16 10:06 PM

Great, this worked in my site like a charm after searching around in various websites for a solution.

 avatar
#20 Paulo said...
04-Feb-16 3:52 PM

Finnally it works!!! Using the rewrite technique explained in the post, and this code, finnally works in all conditions:

protected void Application_BeginRequest() { #region Godaddy shared host fix - Detect VDIR in url and remove

        //verified that HTTP_X_ORIGINAL_URL keeps the original url (withoud domain) before url rewrite module,
        //that way can check if the virtual directory name is at start, and remove it.
        if (Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL"))
        {
            var origUrl = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
            var matchVdir = "/" + Myproj.Core.Constants.Environment.HostingVirtualDirectoryName + "/";

            if (origUrl.StartsWith(matchVdir))
            {
                var urlFix = Request.Url.GetLeftPart(UriPartial.Authority) + "/" + origUrl.Remove(0, matchVdir.Length);
                log.Info("3. Application_BeginRequest fix vdir, url: " + urlFix);
                Response.RedirectPermanent(urlFix);
            }
        }
        #endregion
    }
Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response