views:

74

answers:

3

Hello.

Consider the following problem, please. I have got domain.tld with hierarchical sub-domains like the following:

a.domain.tld
b.a.domain.tld
c.b.a.domain.tld
... etc.

There is also hypothetical directory structure in the web-root:

/foo
/a.
/a./b.
/a./b./bar
/a./b./c.
... etc.

I would like to achieve such rewrite that would cause Apache to serve directories in a way you see below.

a.domain.tld      -->  /a.
b.a.domain.tld    -->  /a./b.
c.b.a.domain.tld  -->  /a./b./c.
... etc.

Directories without trailing dot character would behave as normal sub-directories.

domain.tld/foo/    --> /foo
a.b.domain.tld/bar --> /a./b./bar

I can not use mod_vhost_alias and would be glad if the solution was achievable using mod_rewrite only. Is it possible to achieve such rewrite?

Thank's for all your advices.

--
nkd

A: 

It is possible if you send the part to be reversed to an external script via RewriteMap and have that handle it.

Ignacio Vazquez-Abrams
Hello. Thank you for your fast response. I know of RewriteMap. I would like rather not use it, if possible. Is it?
nkd
There is no built-in facility of mod_rewrite that splits, reverses, and then joins.
Ignacio Vazquez-Abrams
Thanks. So that mean I need to write 10 sets of rules for, say, 10 levels of sub-domains, right? Can anyone show me how to do it right for, say, first two levels (I will derive next 8 easily myself, I think). The hierarchy will not overlap about 10 or 12 levels.
nkd
`RewriteCond %{HTTP_HOST} ([^\.]+)\.example\.com` `RewriteRule (.*) %1./$1 [R,L]` `RewriteCond %{HTTP_HOST} ([^\.]+)\.([^\.]+)\.example\.com` `RewriteRule (.*) %2./%1./$1 [R,L]` Have fun.
Ignacio Vazquez-Abrams
Shouldn't there be ... ^([^\.]+)\.example\.com ... etc.in the RewriteCond? (The beginning "^")
nkd
Yeah, that would probably help.
Ignacio Vazquez-Abrams
A: 

Possible solution for 4 levels of sub-domains:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld
RewriteRule (.*) %1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %3./%2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %4./%3./%2./%1./$1 [R,L]

Thank you.

--
nkd

nkd
A: 

Hello.

The previous solution ends in very funny infinite redirect loop. Here's a solution I got now (not very elegant, but it works; but with a huge 'but'):

# Working solution for five levels of sub-domains

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC]

RewriteRule (.*) http://DOMAIN.TLD/%5./%4./%3./%2./%1./$1 [R,L]

Can somebody explain to me why (the hell) it works? It really does work, I tested it extensively. But why does it work actually? If I look at the RewriteRule line I doubt it should work... Thank you for your explanations.

BTW: If the above five rewrite conditions and rule work, it looks like it could be re-written in some sort of 'two-liner' (one condition plus one rule). I tried that already, by using the above rule and the following condition instead of the five conditions given above:

RewriteCond %{HTTP_HOST} ^(([^\.]+)\.)+DOMAIN\.TLD [NC]

and played with it a little but with no real success. Thanks for all ideas how to simplify the rewrite stuff and make it more 'sane' (if possible).

--
nkd

nkd