photo © 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.]
[LATER STILL UPDATE 06-Feb-2017: the CodeBeater site has been taken over and is now trying to serve up a scam/malware.]
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!
Now playing:
Dido - Northern Skies (Remix)
(from Safe Trip Home)
20 Responses
#1 Steve said...
03-Jan-11 4:52 PMI 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.
#2 Eric said...
09-Jan-11 12:50 PMThanks, 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
#3 Leonardo Lima said...
30-Jan-11 2:32 PMI´ve the same problem, but I´m using Windows 2003 Server and IIS 6, anyone can help to solve this issue ?
#4 Greg said...
06-Feb-11 9:39 PMI 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.
#5 julian m bucknall said...
07-Feb-11 7:27 PMAll: 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
#6 CodeBeater said...
20-Feb-11 12:25 PMJulian,
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
#7 CodeBeater said...
20-Feb-11 12:26 PMForgot to include a link to the post: www.codebeater.com/.../Solution-for-AS
#8 julian m bucknall said...
21-Feb-11 8:36 AMCodeBeater (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
#9 stasevich said...
21-Feb-11 2:36 PMworks like a charm, spent 2+days looking for solution, trying different thing, got no help from GODADsters...
where can i read more on this?
#10 stasevich said...
21-Feb-11 2:46 PMworks great!
#11 chris patterson said...
24-Feb-11 1:06 PMIt 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:
#12 Ian cullen said...
30-May-11 8:57 AMadding this to my config file helped me
or
#13 Kashmir said...
01-Oct-11 3:15 AMThe above said solution is not working for asp.net 4.0.
#14 Bikram said...
31-Mar-12 7:02 AMBut 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
#15 Markus said...
05-Aug-12 7:16 PMThis 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.
#16 Ashoka said...
21-Aug-12 12:20 AMThis is not working now.. I have posted this issue in codeplex blogengine forum too here > blogengine.codeplex.com/.../392425
#17 Varun Sharma said...
16-Aug-13 10:53 AMHi 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:
but not working.
what to do.
THis is not mvc but pure asp.net
#18 Paulo Gonçalves said...
17-Jun-15 5:07 AMVarun 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.
#19 Arun Antony said...
21-Jan-16 10:06 PMGreat, this worked in my site like a charm after searching around in various websites for a solution.
#20 Paulo said...
04-Feb-16 3:52 PMFinnally 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
Leave a response
Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.
_emphasis_
**strong**
[text](url)
`IEnumerable`
* an item
1. an item
> Now is the time...
Preview of response