Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to use domains like xxx.abc.domain.tld.
The xxx is my folder to access. I tried it with the rewrite rules, but I can't get it working, because I don't know how to get the part xxx from the SERVER_NAME into my RewriteRule.

This was my try:

UseCanonicalName Off

# include the IP address in the logs so they may be split
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /var/log/apache2/vaccess.log vcommon

RewriteEngine On

# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower

## deal with normal documents first:
# do the magic
RewriteCond ${lowercase:%{SERVER_NAME}} ^.+\.abc\.domain\.tld$
RewriteRule ^(.*)$ /var/www/abc.domain.tld/[xxx-part]/$1 [L]

Perhaps there is a better solution.
In generally I want to create a dynamic login system with mod_auth_mysql and for each xxx is a seperate user database.
I would prefer the domain/address syntax abc.domain.tld/xxx, but I don't know how to realize it.

Thanks for any advices.

share|improve this question
add comment

migrated from serverfault.com Feb 21 '11 at 21:42

This question came from our site for professional system and network administrators.

2 Answers

Would it not be easier to inject the hostname into the rewrite rule via a RewriteCond back-reference?

UseCanonicalName Off

# include the IP address in the logs so they may be split
LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /var/log/apache2/vaccess.log vcommon

RewriteEngine On

# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower

## deal with normal documents first:
# do the magic
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)\.abc\.domain\.tld$
RewriteRule ^(.*)$ /var/www/abc.domain.tld/%1/$1 [L]

See mod_rewrite documentation, under RewriteRule "back-references." Notice the use of the %1 instead of the $1 backreference. This should grab the contents of the regular expression sub-match in the RewriteCond above. I have added parenthesis to submatch the 'xxx' portion in the RewriteCond.

share|improve this answer
    
I want to use the 'xxx' portion to determine which system shall be loaded. Is there any possibility to let run a mysql basic auth with svn and trac? Because I don't know how to replace the <Location /svn> with mod_rewrite. –  H3llGhost Feb 18 '11 at 22:16
    
Now I'm really confused ... You spoke of xxx -> folder in your question. Maybe you should edit the question and be more specific about everything you're trying to accomplish. Url is broken up as scheme://hostname:port/path?query. mod_rewrite can only be used to manipulate everything after the hostname:port. I can't think of any way to create a single virtual host configuration which covers so many varied hosts and still provides a configuration for Auth, SVN, Trac etc. In short, you're trying to accomplish too many things at once. –  Jeff Hall Feb 19 '11 at 2:20
    
There were two ideas for the url, xxx.abc.domain.tld and abc.domain.tld/xxx. I choiced now for abc.domain.tld/xxx because I need only two hosting settings, with the rewriterule adapted from yours I wrote a new rule, which match my url, but how can I pass a environmental variable to AuthMySQL_DB? –  H3llGhost Feb 19 '11 at 16:53
    
... but how can I pass a the 'xxx' part as database to AuthMySQL_DB svn_[xxx part]_ and as part of the svn repo SVNPath /var/svn/[xxx part]? –  H3llGhost Feb 19 '11 at 17:22
    
To avoid more confusion, I mark this question as solved, because your answer has solved the question. –  H3llGhost Feb 20 '11 at 0:30
add comment

If the URLs that are being used contain the xxx as part of the server name then it's possible to use Name Based virtual Hosts e.g.

If you have xxx.abc.domain.tld and yyy.abc.domain.tld then you could setup a virtual host for each e.g.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName xxx.abc.domain.tld
    DocumentRoot /var/www/abc.domain.tld/xxx
</VirtualHost>

<VirtualHost *:80>
    ServerName yyy.abc.domain.tld
    DocumentRoot /var/www/abc.domain.tld/yyy
</VirtualHost>

and so on.

If you want to use abc.domain.tld/xxx, abc.domain.com.tld/yyy etc. then just set up a single host, define the DocumentRoot and the /xxx, /yyy etc would just be directories under DocumentRoot.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.domain.tld
    DocumentRoot /var/www/abc.domain.tld
</VirtualHost>

Now, abc.domain.tld/xxx would be directed to /var/www/abc.domain.tld/xxx and so on.

share|improve this answer
    
Thanks for your comment. That is the system I used before, but hosting and managing a lot of subdomains is very circular. There is one domain with a lot of subdomains containing two locations. One of the location use the mod_wsgi and the other mod_dav_svn. It should be dynamically. –  H3llGhost Feb 17 '11 at 17:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.