Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i have an issue with my sub-subdomains on my server(Apache 2.2 on Debian), i have this line in my /etc/apache2/sites-available:

RewriteRule ^(([^.\/]+.)?site.com)/(.*) /www/site.com/www/root/$3 [L]

and this line should mean every subdomain or sub-subdomain (and so on) must be redirected to the directory above, in this directroy i have an .htaccess file which get the requests.

Everithing is working fine when i type site.com and subdomain.site.com, but the problem comes when i try to use sub-subdomain like m.subdomain.site.com (and even www.subdomain.site.com) it gives me 400 Bad Request Error message. I tried to redirect www.subdomain.site.com .htaccess file but no result (it seems it is not even checked) and i think the error is in the rewrite rule above and it do not redirect properly the request to the directory.

Can somebody gives a hint whre the problem is?

EDIT: Here is the whole vhost configuration in sites-available:

NameVirtualHost 1.2.3.4:80
NameVirtualHost 1.2.3.4:443

<VirtualHost 1.2.3.4:80>
    ServerName mysite.com
    ServerAdmin admin@mysite.com

    DocumentRoot /www

    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    RewriteEngine On

    RewriteMap  lowercase  int:tolower
    RewriteRule ^(.+)  ${lowercase:%{SERVER_NAME}}$1  [C]
    RewriteRule ^(([^\.\/]+\.)?othersite1\.com)/(.*) /www/othersite1.com/www/root/$3  [L]
    RewriteRule ^(([^\.\/]+\.)?othersite2\.com)/(.*) /www/othersite2.com/www/root/$3        [L]
    RewriteRule ^(([^\.\/]+\.)?mysite.com)/(.*) /www/mysite.com/www/root/$3        [L]
    RewriteRule ^(([^\.\/]+\.)?othersite3\.com)/(.*) /www/othersite3.com/www/root/$3        [L]
    RewriteRule ^(([^\.\/]+\.)?othersite4\.com)/(.*) /www/othersite4.com/www/root/$3        [L]
    RewriteRule ^(([^\.\/]+\.)?[^\.\/]+\.[^\.\/]+)/(.*) /www/mysite.com/www/root/$3        [L]

#    RewriteMap  lowercase  int:tolower
#    RewriteRule ^(.+)  ${lowercase:%{SERVER_NAME}}$1  [C]
#    RewriteRule ^([^\.\/]+\.[^\.\/]+)/(.*) /www/$1/www/root/$2
#    RewriteRule ^www\.([^\/]+)\.([^\.\/]+\.[^\.\/]+)/(.*) /www/$2/www/root/$3
#    RewriteRule ^([^\/]+)\.([^\.\/]+\.[^\.\/]+)/(.*) /www/$2/www/root/$3
</VirtualHost>

<VirtualHost 1.2.3.4:443>
    ServerName secure.mysite.com
    ServerAdmin admin@mysite.com

    DocumentRoot /www

    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    SSLEngine on
    SSLCertificateFile       /etc/ssl/certs/mysite/secure.mysite.com.crt
    SSLCertificateKeyFile    /etc/ssl/certs/mysite/secure.mysite.com.key
    SSLCertificateChainFile  /etc/ssl/certs/mysite/gd_bundle.crt

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>

    BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

    RewriteEngine On
    RewriteMap  lowercase  int:tolower
    RewriteRule ^(.+)  ${lowercase:%{SERVER_NAME}}$1  [C]
    RewriteRule ^([^\.\/]+\.[^\.\/]+)/(.*) /www/$1/www/root/$2
    RewriteRule ^www\.([^\/]+)\.([^\.\/]+\.[^\.\/]+)/(.*) /www/$2/www/root/$3
    RewriteRule ^([^\/]+)\.([^\.\/]+\.[^\.\/]+)/(.*) /www/$2/www/root/$3
</VirtualHost>

There are multiple sites located on differents subdomain and everyone of them has mobile version located at m.subdomain.mysite.com

share|improve this question
add comment

3 Answers 3

up vote 0 down vote accepted

Okay, so i made some tests about your case, and here is the working result (i don't think the current RewriteRules , you are using at this time are correct - these showed above).

So here is my tested solution. Example 1 shows rewriting of fisrt level subdomain (eg. subdomain.domain.com), and example 2 shows rewriting second level subdomains (eg. subdomain.subdomain.domain.com).

Here are the examples: Example 1:

1. RewriteEngine On
2. RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
3. RewriteCond /var/www/html/%1 -d
4. RewriteRule ^(.*) /%1/$1 [L]
    • Enable rewrite engine
    • catch the 1st level subdomain
    • check if the directory (the name of the subdomain) exists
    • rewrite the requested file to the directory (the given example uses relative paths)

Example 2:

1. RewriteEngine On
2. RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.com
3. RewriteCond /var/www/html/%1/%2 -d
4. RewriteRule ^(.*) /%1/%2/$1 [L]
    • Enable rewrite engine
    • catch the 1st/2nd level subdomains
    • check if the directory (the name of the subdomains) exists
    • rewrite the requested file to the directory (the given example uses relative paths)

So here is the result from the given examples above:

Ex. 1 - Hit url test.domain.com/index.php --> redirects to --> /var/www/html/test/index.php

Ex. 2 - Hit url 1.2.domain.com/index.php --> redirects to --> /var/www/html/1/2/index.php

I assume, that the virtualhost is configured with document root of - /var/www/html

share|improve this answer
    
This was it! You saved my week! Thank you for your help, i really appreciate it :)! –  SugarSick Oct 8 '10 at 7:40
add comment

Are you sure, your Apache is configured properly to serve the so-called - sub subdomains ? I mean, are these subdomains are preconfigured ones , for ex. (sub-sub1, sub-sbu2 ..finite number , configured in the vhost config), or these are random sub-subdomains ? If they are random, I suppose you've got some wildcards in the vhost's config ?

--------=============----------

Okay so, try adding ServerAlias *.mysite.com ServerAlias *.othersite1.com ....... ServerAlias *.othersite[N].com

in your vhost configuration

share|improve this answer
    
hi, i have edited my question and added some additional info. –  SugarSick Oct 1 '10 at 10:39
    
Hi, have you checked my answer ? –  gryzli Oct 2 '10 at 10:54
    
hello, thanks for your answer, but ServerAlias directive doesn't work, i added it just after ServerAdmin in the port:80 part –  SugarSick Oct 4 '10 at 13:07
    
what do you think about this line: RewriteRule ^(([^\.\/]+\.)?mysite.com)/(.*) /www/mysite.com/www/root/$3 , the guy who coded it told me the part ([^\.\/]+\.) catches all level subdomains, is it true? –  SugarSick Oct 4 '10 at 13:45
add comment

This sounds like a job for mod_vhost_alias or even simpler seperate name-based virtual hosts with proper ServerAlias directives which is far more maintainable than your take with mod_rewrite.

share|improve this answer
    
i tried ServerAlias directives but it doesn't work for me with this configuration file (i agree it is not the best) –  SugarSick Oct 4 '10 at 13:11
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.