Note: This article was originally published in 2009. Some steps, commands, or software versions may have changed. Check the current General documentation for the latest information.

Currently I am working on ReWriting some URLs based on the subdomain name provided (e.g. you type Restaurants.JCBauza.com and it takes you to a page that is dedicated to restaurants.) Seeing that different content lives on different directories, I wanted to facilitate the process of URL Rewriting from the administrator’s perspective. 

The issue: When rewriting an URL you have to map Restaurants to /wikis/ListofRestaurants/ or something like that. Let’s pretend you have a large list of restaurants (Restaurant A, B, C, and so forth) and they all live on /Restaurants/NameofRestaurant.aspx. The issue here is that as the path of the folder increases in length, having to write that down for every mapping makes it really hard to read, and as you increase the offerings in your site (e.g. Restaurants, Bars, Dealerships, Airports, Hotels, Books, etc) your URLS are all different. In order to decrease the mess I created 3 files that hold different kind of mappings. The first one named “Folders.txt” is a list of the directories or FOLDERS were the pages leave and a key to find them (let’s call it TYPE). Example, TYPE “RESTAURANTREVIEW” is mapped to “/wiki/Restaurants”. File “Types.txt” holds all the possible SUBDOMAINS and then a TYPE associated to them. So, SUBDOMAIN RestaurantA is mapped to TYPE “RESTAURANTREVIEW”. Then finally, file “URLMap.txt” would hold the SUBDOMAIN and the PAGE you need to go to. So SUBDOMAIN “RestaurantA” will go to page “RestaurantA.aspx”. Then the trick is to have an URLMapping from RestaurantA to both the page and the folder. 

The solution: Nested lookup calls. By writing a RewriteRule you can do a nested call to the different mappings so that a call for RestaurantA.JCBauza.com will grab the RestaurantA part of the URL request, find from the mappings that a) it is a RESTAURANTREVIEW and b) the page is RestaurantA.aspx. After that, the nested call will map RESTAURANTREVIEW to the actual directory it has to go to “/wiki/Restaurants”.

The remaining problem: We have 3 files we have to maintain. Folders.txt is not a huge deal as it won’t be changing often, but UrlMap.txt and Types.txt need to be edited every time you add a new subdomain. Maybe there is an easier way but for now I think this will take care of having a more presentable file as well as facilitating changing the path (Folder) were the files reside with only 1 change (instead of a potentially dangerous find and replace).

Here is a sample call to the three files:

RewriteRule .? ${Folders:${Types:%2|}|}${URLMap:%2|search/SearchResults.aspx?q=%2}