Domain name-based rewrite

Topics: User Forum
Nov 9, 2011 at 2:37 PM

I have seen a ton of examples for URL rewrites based on the subdomain, but I am trying to do URL rewrites based on the domain name itself, specifically serve content from a subfolder with the domain name in it.

For instance, I'd like to be rewritten to either:
/sites/ or

I've been able to do a lot of the IIRF rewrite regexs on my own, but I am hitting a brick wall on this one.


Nov 9, 2011 at 10:23 PM

This is in the documentation, under "Examples...Route by Hostname"

# With a request like, rewrite the request
# to /  This assumes there is a vdir set
# up in IIS  for each hostname.   If the server is not configured with a
# vdir named "", then IIS will return a 404 "Not Found".

RewriteRule ^/(?!host-)(.*)$ /host-%{HTTP_HOST}/$1
Nov 10, 2011 at 2:27 AM
Edited Nov 10, 2011 at 2:31 AM

Thanks so much for the reply.  This is CLOSE to the idea, but not quite right. (If I am reading you right)

The hostname should always be "www" by a previous redirect rule.  The domain name is what changes,

so => or if that is two difficult, then:  =>

So these folders would actually have to exist - I can't use vdirs.  So if I basically would like to be able to pull these folders out and have them still work if need be.

Thanks again!!

Nov 10, 2011 at 3:32 PM

>  So if I basically would like to be able to pull these folders out and have them still work if need be.

I don't know what that means.

The rule I gave is pretty much exactly what you need, except you should replace host- with sites/ , if that's how you want to shape your URLs.

I think you need to play with it a little. You should be able to figure it out.


Nov 10, 2011 at 3:46 PM

Sorry, you can smack me if you ever meet me.  That totally works!  Next paycheck, I'll hit your charity donation.  You're the author of IIRF, right?  If so, I've been using it while developing our startup and love it.  Been doing a lot of other stuff with it, but this one was just perplexing me for some reason.

Thanks again.

Nov 10, 2011 at 3:51 PM

Yeah, I wrote it.

Glad you like it, and glad you figured it out.

Good luck with your startup!

Nov 10, 2011 at 4:03 PM

Along the same lines, with a variable domain name if I want to do a forced "www" - I tried the example that utilizes the %{HTTP_HOST} variable and I get a time out (when combined in the same INI file as the last.  So my config looks like this:

RewriteLog c:\logs\iirf
RewriteLogLevel 1
IterationLimit 10
MaxMatchCount 10
RewriteEngine ON
StatusInquiry ON

RewriteCond %{HTTP_HOST} ^(?!www)[^\.]+\.[^\.]+$ [I]
RedirectRule ^/(.*) http://www.%{HTTP_HOST}/$1 [R=301]

RewriteRule ^/(?!sites/)(.*)$ /sites/%{HTTP_HOST}/$1


Anything obvious you see I am doing wrong?  

Nov 10, 2011 at 9:22 PM

No, I don't see anything obvious. It may be easier to diagnose if you set RewriteLogLevel to 3 and send the excerpt of the IIRF log for the request that times out.


Nov 10, 2011 at 9:52 PM
Edited Nov 10, 2011 at 9:55 PM

I forgot to put '' in my hosts file, so hence the timeout.... I think I have it going.

Thanks again, SO much!