all posts

Links, Subfolders, ISAPI_Rewrite, Routing, and more

Published to Blog on 6 Apr 2010

I know that is a horrible title but “How to fix your generated links that contain an extra subfolder name when using ISAPI_Rewrite to host more than one site on your OrcsWeb hosting account” seemed a little long. Now that I’ve stuffed as many keywords as I can into the first paragraph let’s get on with the story…

I am smarter than Jamie Wright, Tim Wingfield, Nate Kohari and James Avery combined! At least according to Michael Eaton:

Awesome! @DanHounshell helped me figure something out that stumped @jwright, @TimWingfield, @nkohari and @averyj :-) Good times.

Okay, I doubt that I’m smarter than any of them but in this case I did know the answer to the problem that Mike had stumbled upon… because I’d seen and solved it before – nearly two years ago.


Problem

Almost exactly two years ago Keith Elder ran into some issues setting up a Graffiti site on his hosting account with OrcsWeb. He thought it was Graffiti-related so he asked for my help. If I remember correctly the issue stemmed from trying to run multiple sites from one hosting account using ISAPI_Rewrite to redirect requests for the second domain to an application that was in a sub-folder of the main domain. That might be a little confusing, the below example should help.

Example:

You have a site setup in your hosting account’s root folder.

You have a site (application) setup in hour hosting account’s root/sitetwo folder.

www.siteone.com points to the hosting account’s root folder.

www.sitetwo.com points to the hosting account’s root folder.

Use ISAPI_Rewrite to make all requests for www.sitetwo.com point to the hosting account’s root/sitetwo folder.

This actually works quite well and all requests to www.sitetwo.com get “routed” to the root/sitetwo folder. The issue is that the “real” url for for the files in the SiteTwo website is still “http://sitetwo.com/sitetwo/…”. Any link that you do not manually create on sitetwo (especially when using ASP.NET routing) will have the “real” url - ASP.NET/IIS doesn’t care about ISAPI_Rewrite’s sleight of hand. For example the link in a menu to a contact us page ends up looking like “http://www.sitetwo.com/sitetwo/contactus.aspx” instead of the expected “http://sitetwo.com/contactus.aspx”.

Keith and I worked through it for a couple of hours and I came up with a solution for him that seemed to work well enough. At the time I didn’t think to blog about the problem/solution because it seemed like a once in a lifetime type of thing.

Fast forward nearly two years and Michael Eaton IMs to ask a question about routing and htaccess files. At first I was stumped but I asked for more details which triggered the gears to start turning and memories of something similar began surfacing from the scary places of my brain. Turns out Mike was having the exact same problem that Keith ran into previously!

So now that you know the back story I’m sure you are just dying to know the answer that we came up with aren’t you? It’s actually so simple that I’m afraid you’ll feel letdown after such a big build-up. If you do feel let down you can visit the following site for a quick pick-me-up: http://www.mysecretisimawesome.com/


Solution

The solution is to remove the extra folder name in the urls from the output before it is sent to the client. Create an HTTPModule that uses a response filter to replace instances of one string in the output with another. Create a custom ConfigurationSection for defining the pairs of strings to replace. That’s it. Replace the junky stuff in the output stream with good stuff. Told you it was easy.

In the example case above we would configure the output to replace any instance of “/sitetwo/” with “/”. Now all links created for sitetwo.com would be formatted “properly” for the end users, as http://www.sitetwo.com/… rather than http://www.sitetwo.com/sitetwo/….

In Mike’s case he configured “/melons/” to be replaced with “/” so that his menu links would be sent to the client looking like “http://LookAtTheSizeOfThatGuysMelon.com/averyj.aspx” rather than “http://LookAtTheSizeOfThatGuysMelon.com/melons/averyj.aspx”.


Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, mobile, motorcycles, watches, food, Nashville, Savannah, Cincinnati and family. Dislikes: mean people
Dan Hounshell on Twitter