sub domain exclusion

Topics: Developer Forum, User Forum
Jun 14, 2007 at 2:45 PM
Hi all,

I used mod_rewrite up to installing vista and have now switched to iirf. I have the following rules:
RewriteLog c:\inetpub\rewrite\log_iirf
RewriteLogLevel 5

RewriteCond %{HTTP_HOST} ^(!www)\.(^.)\.(^.)$
RewriteCond %{HTTP_HOST} ^(!setup)\.(^.)\.(^.)$
RewriteRule ^(.)/(^/+)//(.)$ $1/$3 U,I,N
RewriteRule ^(.)/blog/(.)/(.)\.htm(.)$ $1/blog/$2& U,I
RewriteRule ^(.)/blog/(.)/(^/)/(^/)$ $1/blog/$2&$3=$4 U,I,N
RewriteRule ^(.)/blog/(^&+)&(.)$ /$1/$2.asp?$3 U,I
RewriteRule ^(.)//(.)$ $1/$2 U,I

RewriteCond %{HTTP_HOST} ^(!www)\.(^.)\.(^.)$
RewriteCond %{HTTP_HOST} ^(!setup)\.(^.)\.(^.)$
RewriteRule ^/(.*)$ /%2/$1 U,I,L


What it should do:
user.domain.com --> /domain
setup.domain.com --> /
www.domain.com --> /
user.domain.com/blog/showitem/id/1/rsp/2/var/3/title.html --> /domain/showitem.asp?id=1&rsp=2&var=3
user.domain.com/members/blog/edit/id/1/rsp/2/title.html --> /domain/members/edit.asp?id=1&rsp=2

user can be any name! If it is not found in the database, it is redirected to setp.domain.com
i added the RewriteRule ^(.)//(.)$ $1/$2 U,I to fix a double slash problem caused by among others empty
variables...

However, it does not work. What am i missing? I know the 'N' flag is not used here, but it should run the rule untill it can't run any more...

Kind regards,
Alexander Lange
Coordinator
Jun 14, 2007 at 11:28 PM
"It does not work"

what do you see?
what does the logfile indicate?
Jun 27, 2007 at 7:37 AM
It either rewrites setup.domain.nl to the correct place (root of the default website) or it rewrites username.domain.nl to the correct place (defaultwebsite/username) not both. Logfile for the first situation:

Wed Jun 27 08:30:15 2007 - ApplyRules (depth=0)
Wed Jun 27 08:30:15 2007 - Rule 1 : -1 (No match)
Wed Jun 27 08:30:15 2007 - Rule 2 : -1 (No match)
Wed Jun 27 08:30:15 2007 - Rule 3 : -1 (No match)
Wed Jun 27 08:30:15 2007 - Rule 4 : -1 (No match)
Wed Jun 27 08:30:15 2007 - Rule 5 : -1 (No match)
Wed Jun 27 08:30:15 2007 - Rule 6 : 2 matches
Wed Jun 27 08:30:15 2007 - GetServerVariable: getting 'HTTP_HOST'
Wed Jun 27 08:30:15 2007 - GetServerVariable: xferred 18 bytes
Wed Jun 27 08:30:15 2007 - GetServerVariable: result 'setup.domain.nl'
Wed Jun 27 08:30:15 2007 - ReplaceServerVariables: VariableName='HTTP_HOST' Value='setup.domain.nl'
Wed Jun 27 08:30:15 2007 - ReplaceServerVariables: InputString='%{HTTP_HOST}' out='setup.domain.nl'
Wed Jun 27 08:30:15 2007 - GenerateReplacementString: src='/favicon.ico','(null)' ReplacePattern='setup.domain.nl' vec=[[ [ 0, 12, 1, 12] [] ] counts=2,0
Wed Jun 27 08:30:15 2007 - EvaluateCondition: checking 'setup.domain.nl' against pattern '^(!www)\.(^.)\.(^.)$'
Wed Jun 27 08:30:15 2007 - Cond : -1 (No match)
Wed Jun 27 08:30:15 2007 - Cond : Child is non NULL
Wed Jun 27 08:30:15 2007 - Cond : Current condition evaluates to FALSE
Wed Jun 27 08:30:15 2007 - Cond : Logical AND, ergo no need to evaluate Child condition
Wed Jun 27 08:30:15 2007 - EvaluateConditionList: FALSE, Rule does not apply
Wed Jun 27 08:30:15 2007 - ApplyRules: returning 0
Wed Jun 27 08:30:15 2007 - No Rewrite
Wed Jun 27 08:30:15 2007 - HttpFilterProc SFNOTIFYLOG
Wed Jun 27 08:30:15 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - OnUrlMap: storing physical path (C:\inetpub\wwwroot\weblog), in ptr (0x033a4d70)
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYAUTH_COMPLETE
Wed Jun 27 08:30:20 2007 - DoRewrites
Wed Jun 27 08:30:20 2007 - New Url: '/'
Wed Jun 27 08:30:20 2007 - ApplyRules (depth=0)
Wed Jun 27 08:30:20 2007 - Rule 1 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 2 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 3 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 4 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 5 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 6 : 2 matches
Wed Jun 27 08:30:20 2007 - GetServerVariable: getting 'HTTP_HOST'
Wed Jun 27 08:30:20 2007 - GetServerVariable: xferred 22 bytes
Wed Jun 27 08:30:20 2007 - GetServerVariable: result 'username.domain.nl'
Wed Jun 27 08:30:20 2007 - ReplaceServerVariables: VariableName='HTTP_HOST' Value='username.domain.nl'
Wed Jun 27 08:30:20 2007 - ReplaceServerVariables: InputString='%{HTTP_HOST}' out='username.domain.nl'
Wed Jun 27 08:30:20 2007 - GenerateReplacementString: src='/','(null)' ReplacePattern='username.domain.nl' vec=[[ [ 0, 1, 1, 1] [] ] counts=2,0
Wed Jun 27 08:30:20 2007 - EvaluateCondition: checking 'username.domain.nl' against pattern '^(!www)\.(^.)\.(^.)$'
Wed Jun 27 08:30:20 2007 - Cond : -1 (No match)
Wed Jun 27 08:30:20 2007 - Cond : Child is non NULL
Wed Jun 27 08:30:20 2007 - Cond : Current condition evaluates to FALSE
Wed Jun 27 08:30:20 2007 - Cond : Logical AND, ergo no need to evaluate Child condition
Wed Jun 27 08:30:20 2007 - EvaluateConditionList: FALSE, Rule does not apply
Wed Jun 27 08:30:20 2007 - ApplyRules: returning 0
Wed Jun 27 08:30:20 2007 - No Rewrite
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - OnUrlMap: storing physical path (C:\inetpub\wwwroot\weblog\Default.asp), in ptr (0x0338dce8)
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYLOG
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - OnUrlMap: storing physical path (C:\inetpub\wwwroot\weblog\domain_files\domainmain.css), in ptr (0x004ef2f0)
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYURL_MAP
Wed Jun 27 08:30:20 2007 - OnUrlMap: storing physical path (C:\inetpub\wwwroot\weblog\ajax.js), in ptr (0x0338de38)
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYAUTH_COMPLETE
Wed Jun 27 08:30:20 2007 - DoRewrites
Wed Jun 27 08:30:20 2007 - New Url: '/domain_files/domainmain.css'
Wed Jun 27 08:30:20 2007 - ApplyRules (depth=0)
Wed Jun 27 08:30:20 2007 - Rule 1 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 2 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 3 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 4 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 5 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 6 : 2 matches
Wed Jun 27 08:30:20 2007 - GetServerVariable: getting 'HTTP_HOST'
Wed Jun 27 08:30:20 2007 - GetServerVariable: xferred 22 bytes
Wed Jun 27 08:30:20 2007 - GetServerVariable: result 'username.domain.nl'
Wed Jun 27 08:30:20 2007 - ReplaceServerVariables: VariableName='HTTP_HOST' Value='username.domain.nl'
Wed Jun 27 08:30:20 2007 - ReplaceServerVariables: InputString='%{HTTP_HOST}' out='username.domain.nl'
Wed Jun 27 08:30:20 2007 - GenerateReplacementString: src='/domain_files/domainmain.css','(null)' ReplacePattern='username.domain.nl' vec=[[ [ 0, 32, 1, 32] [] ] counts=2,0
Wed Jun 27 08:30:20 2007 - EvaluateCondition: checking 'username.domain.nl' against pattern '^(!www)\.(^.)\.(^.)$'
Wed Jun 27 08:30:20 2007 - Cond : -1 (No match)
Wed Jun 27 08:30:20 2007 - Cond : Child is non NULL
Wed Jun 27 08:30:20 2007 - Cond : Current condition evaluates to FALSE
Wed Jun 27 08:30:20 2007 - Cond : Logical AND, ergo no need to evaluate Child condition
Wed Jun 27 08:30:20 2007 - EvaluateConditionList: FALSE, Rule does not apply
Wed Jun 27 08:30:20 2007 - ApplyRules: returning 0
Wed Jun 27 08:30:20 2007 - No Rewrite
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYLOG
Wed Jun 27 08:30:20 2007 - HttpFilterProc SFNOTIFYAUTH_COMPLETE
Wed Jun 27 08:30:20 2007 - DoRewrites
Wed Jun 27 08:30:20 2007 - New Url: '/ajax.js'
Wed Jun 27 08:30:20 2007 - ApplyRules (depth=0)
Wed Jun 27 08:30:20 2007 - Rule 1 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 2 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 3 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 4 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 5 : -1 (No match)
Wed Jun 27 08:30:20 2007 - Rule 6 : 2 matches


Alexander
Coordinator
Jun 27, 2007 at 8:20 PM
Rather than me going through your logfile and trying to understand the rules as you've written them, I'd like to understand what it is you want the rules to do.

You wrote your first rule as:
RewriteCond %{HTTP_HOST} ^(!www)\.(^.)\.(^.)$
RewriteCond %{HTTP_HOST} ^(!setup)\.(^.)\.(^.)$
RewriteRule ^(.)/(^/+)//(.)$ $1/$3 U,I,N

I am pretty sure that is not what you meant, because (^.) doesn't make sense to me. I think what you pasted was right, but the forum engine is interpreting your rule string as markup. If you want to enclose rules in these posts you need to enclose the block of text in double-curly braces, to prevent interpretation of the rule string as markup. like this:

 { {
    text here
 } }

(Include no spaces between the curlies.) Assuming I am right about the markup, I think your first two RewriteCond statements are probably more like this:
RewriteCond %{HTTP_HOST} ^(?!www)\.([^\.]+)\.([^\.]+)$
RewriteCond %{HTTP_HOST} ^(?!setup)\.([^\.]+)\.([^\.]+)$

Is that right?

What you seem to want is to exclude www.domain.com, and setup.domain.com, but allow things like snarfle.domain.com and also allow ewww.domain.com, assuming ewww is a valid user name. In that case you probably want a regex like this, in that first RewriteCond:

^(?!\b(www|setup)\b)([^\.]+)\.([^\.]+)\.([^\.]+)$

Looks complicated, eh? Let's take it by parts.

The first part: (?!\b(www|setup)\b). Let's look at the inside of it. \b(www|setup)\b says, match here only if the first word is not www or setup. The \b is a zero-width assertion which matches only on a word boundary. So in this case, www will match, and wwww will not match. That is then wrapped in a (?!...), which is a negative lookahead. It says, match here (but don't capture) only if the inside (...) is NOT matched. In other words, the whole first part only matches if the first word is not www or setup. wwww or ewwww or www3 will all match. www will not match.

The rest of the regex is repetitive. There are three stanzas separated by dots. The ([^\.]+) captures each stanza.

Put that into a RewriteCond like so:
RewriteCond %{HTTP_HOST} ^(?!\b(www|setup)\b)([^\.]+)\.([^\.]+)\.([^\.]+)$

This condition will evaluate to TRUE when the HTTPHOST is not www.anystring1.anystring2_ or setup.anystring1.anystring2 .

That's the condition. Now what is the rule? The rule I think you described is, if the URL has an empty path, then rewrite to a simple URL : /anystring1 . So the complete rule in this case would be:

RewriteCond %{HTTP_HOST} ^(?!\b(www|setup)\b)([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^$  $3 [U,L]

It is a bit curious because EVERY distinct hostname will be rewritten to the same URL. In other words, fred.domain.com will be rewritten to /domain and wilma.domain.com will be rewritten to /domain, and Alexander.domain.com will be rewritten to the same URL, /domain. Is that what you want?

I added the L flag and removed the I and N flags as unnecessary and useless, respectively. But this only matches a URL that has am empty path. You had given these as examples:

# incoming url rewrite result
1 user.domain.com /domain
2 setup.domain.com /
3 www.domain.com /
4 user.domain.com/blog/showitem/id/1/rsp/2/var/3/title.html /domain/showitem.asp?id=1&rsp=2&var=3
5 user.domain.com/members/blog/edit/id/1/rsp/2/title.html /domain/members/edit.asp?id=1&rsp=2

The rule I gave above works for case #1, #2 and #3. Again, those are all empty paths.

Is that what you want?

I would add these as other example outcomes to illustrate the point:
# incoming url rewrite result
6 cheeso.alpha.com /alpha
7 alexander.tempuri.org /tempuri
8 fred.YouKnow.com /YouKnow
9 barney.YouKnow.com /YouKnow

Is that what you want? With the one rule I gave you above, that's what you would get.

We can try to figure out the rest after we deal with this first part.