OrcsWeb – Creating subdomains using ISAPI Rewrite

Over Christmas I moved my blog over to OrcsWeb. I had been meaning to move hosting provider for a while, however just never got around to it. However, in order to make the move as transparent as possible, I needed to create a subdomain of benhall.me.uk.  By default, OrcsWeb does not offer support for subdomains, you can map additional domains onto the account as long as you stay within your account limits, but you can only have one site (In terms of IIS).

However, hats off to the webteam (especially Desirée) at Orcsweb for helping me setup my subdomain, they pointed me towards the direction of ISAPI Rewrite v3 which is setup and ready to go for each site they host. Yesterday, Steve Andrews tweeted about how to do this, so I decided to share the config.

With V3 of ISAPI rewrite, you simply create a file called ‘.htaccess’ in the root of your IIS site. This contains all of your ‘rules’ about how to handle requests.

This is the .htaccess file for my website.

RewriteEngine on

#Redirect rss.xml to feedburner
RewriteCond %{HTTP:Host} blog.benhall.me.uk$
RewriteRule ^(.*)rss.xml$
http://feeds.feedburner.com/BenHall [R=301,NC]

#Fix missing trailing slash char on folders
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]

# this rule directs blog.benhall.me.uk to Sites/blog.benhall.me.uk
RewriteCond %{HTTP:Host} ^(?:blog.)?benhall.me.uk$
RewriteRule ^(.*)$ Sites/blog.benhall.me.uk/$1

The most important section is the last block, this defines that if the request has the HTTP Host address as Blog.BenHall.me.uk, then rewrite the request to return the content from /Sites/blog.benhall.me.uk/ adding additional paths onto the end if required instead.

Now, when you visit blog.benhall.me.uk/index.html, the file is actually returned from /Sites/blog.benhall.me.uk/index.html.

However, for a long time I had a problem in getting this to work as I expected. Because I wanted a seamless experience, I didn’t want /Sites/blog… appearing in the browser’s address bar. It turns out the problem was that in the rule, I was including the full absolute path to the resource, instead of the relative path. By having the IP in the Rule (RewriteRule ^(.*)$ http://0.0.0.0/Sites/blog.benhall.me.uk/$1 ), it was causing a 301 redirect to occur which was being detected by the browser. By having the rule as relative, it would happen under the covers – without the user ever being aware of the change.

Technorati Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *