extras

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)



Posts on similar topics...

18 Responses

  • Mon 03 Jan 2011
  • 4:52 PM
  •  avatar #1

Steve said...

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.

  • Sun 09 Jan 2011
  • 12:50 PM
  •  avatar #2

Eric said...

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

  • Sun 30 Jan 2011
  • 2:32 PM
  •  avatar #3

Leonardo Lima said...

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

  • Sun 06 Feb 2011
  • 9:39 PM
  •  avatar #4

Greg said...

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.

  • Mon 07 Feb 2011
  • 7:27 PM
  • julian m bucknall avatar #5

julian m bucknall said...

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

  • Sun 20 Feb 2011
  • 12:25 PM
  •  avatar #6

CodeBeater said...

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

  • Sun 20 Feb 2011
  • 12:26 PM
  •  avatar #7

CodeBeater said...

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

  • Mon 21 Feb 2011
  • 8:36 AM
  • julian m bucknall avatar #8

julian m bucknall said...

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

  • Mon 21 Feb 2011
  • 2:36 PM
  •  avatar #9

stasevich said...

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?

  • Mon 21 Feb 2011
  • 2:46 PM
  •  avatar #10

stasevich said...

works great!

  • Thu 24 Feb 2011
  • 1:06 PM
  •  avatar #11

chris patterson said...

It looks like this nifty little rewrite rule works because of this function examining the HTTP_X_ORIGINAL_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); }
  • Mon 30 May 2011
  • 8:57 AM
  •  avatar #12

Ian cullen said...

this worked for me:

<modules runAllManagedModulesForAllRequests="true">

  • Mon 30 May 2011
  • 8:57 AM
  •  avatar #13

Ian cullen said...

adding this helped me to my config file

modules runAllManagedModulesForAllRequests=true

  • Sat 01 Oct 2011
  • 3:15 AM
  •  avatar #14

Kashmir said...

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

  • Sat 31 Mar 2012
  • 7:02 AM
  •  avatar #15

Bikram said...

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

  • Sun 05 Aug 2012
  • 7:16 PM
  •  avatar #16

Markus said...

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.

  • Tue 21 Aug 2012
  • 12:20 AM
  •  avatar #17

Ashoka said...

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

  • Fri 16 Aug 2013
  • 10:53 AM
  •  avatar #18

Varun Sharma said...

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

Leave a Response

Some MarkDown is allowed, but HTML is not. (Click to learn more.)

  • 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...

Extras

Search

About Me

I'm Julian M Bucknall, an ex-pat Brit living in Colorado, an atheist, a microbrew enthusiast, a Volvo 1800S owner, a Pet Shop Boys fanboy, a slide rule and HP calculator collector, an amateur photographer, a Altoids muncher.

DevExpress

I'm Chief Technology Officer at Developer Express, a software company that writes some great controls and tools for .NET and Delphi. I'm responsible for the technology oversight and vision of the company.

Validation

Validate markup as HTML5 (beta)     Validate CSS

Bottom swirl