Debian – Rewrite rule for multiple subdomains issue

apache-2.2debianrewrite

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

Best Answer

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

Related Topic