Getting a feel for subdomains and URLRewriting

Dec 13, 2009 at 12:13 AM

Ok, well first of all I apologize because i'm such a noob, I was putting off creating a thread for as long as I could hoping I'd figure out how it's done....but...5 hours later I still can't get what I'm trying to achieve.

For the time being, I'm just trying trying to get [whatever] to rewrite to[whatever].  

I thought that this would do the trick...

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomain\.com [I]
RewriteRule ^/(.*)$ /echo/?echo=%1 [U,L]

I just started learning regular expressions today, but what I get of this, is that the Rewrite condition is basically saying,  match * but doesn't start with www. 

The RewriteRule says, match basically everything, and send it to /echo/?echo=%1.  The problem is that's exactly what it does, its exactly as if I typed in my browser  And my Default.aspx page echos out "%1".  This is not my intent, I want it to echo out the first part of the HTTP_HOST.  Exchanging the %1(percentage sign 1) for $1 echos out whatever I leave trailing on the * for example if I typed it would echo out "hello_there".

So I guess why is the %1 behaving literally?  I searched the help for it, but couldn't find anything describing what that is for, but I see it used in quite a few examples.


My ultimate goal with this subdomain rewriting is to be able to have something like this - rewrites to (actually an extremely long querystring that has nothing to do with he subdomain name), but then and all would be rewritten to and respectively.

But before I try to tackle the regex for those, I thought i'd better try something simple, which is what the above example I'm trying to work out is for. 



Dec 13, 2009 at 12:39 AM
Edited Dec 13, 2009 at 12:43 AM

Kyle, 5 hours?

You deserve a medal for persistence.  Sorry to say, but there's a simple answer. In the prior versions, the % char was the prefix for a capturing group from a RewriteCond.   Because the % is also used as the "escape" character for URL encoding, it is is overloaded - it has multiple meanings.  Therefore, IIRF now uses the * char as the default prefix for the capturing group of a RewriteCond.  You can set the character with CondSubstringBackrefFlag , but in the absence of a that directive that, it's a *.  

So, your solutions are:

  • Replace % with * in the replacement pattern
  • Insert a CondSubstringBackrefFlag directive, specifying % as the backref character.

My apologies - the doc has been inconsistent on this. Some of the examples still use %, which is probably misleading.

next time,  ask sooner...

Dec 13, 2009 at 1:06 AM

There's an example in the doc that does almost exactly what you are doing:

(See example #2 on that page)


Dec 13, 2009 at 1:29 AM

hmm, I'm not sure how I missed that whole section...embarrassing.  That section would have been very helpful a couple hours ago :-).  At least I feel like I learned a lot more about IIRF while digging for a solution.

Thank you very much for your quick reply and help, after that little test I also got my finished script done (I think) all appears to be working I missing anything that is standard to include in this?


RewriteCond %{HTTP_HOST} ^(?!www)subdomain\.myDomain\.com [I]
RewriteRule ^/$ /v1/Default.aspx?tab=1&clientID=826&index=19429&id1=19427&id2=19428&euID=593&browserType= [U,L]
RewriteRule ^/(.+)$ /v1/$1 [U,L]



First condition matches my, but not (case-insensitive).

The 1st Rewrite Rule only rewrites if the request is "/" which should be mean....only match http://subdomain.mysubdomain.

The 2nd Rewrite rule will rewrite any request with more than one character to /v1/[whatever_is_requested]. (so when the Default page needs a css file or something it doesn't look in the wrong place)

I guess i'll get rid of the logging and the U modifiers as well, and then we're good to go for production I think.

If anyone notices anything completely wacky please let me know.

Thanks again Cheeso



Dec 13, 2009 at 1:54 AM

Kyle, your rules look fine.

But they're not what you set out to do originally?  What happened to the %1 or *1 ??

Regardless, If I were you I would put some comments in the ini file to document what you were intending to do.