Friday, January 28, 2011

Rewrite rule for multiple subdomains issue

Hello guys,

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

  • Hi,

    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

    SugarSick : hi, i have edited my question and added some additional info.
    gryzli : Hi, have you checked my answer ?
    SugarSick : hello, thanks for your answer, but ServerAlias directive doesn't work, i added it just after ServerAdmin in the port:80 part
    SugarSick : 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?
    From gryzli
  • 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.

    SugarSick : i tried ServerAlias directives but it doesn't work for me with this configuration file (i agree it is not the best)
    From joschi
  • 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

    SugarSick : This was it! You saved my week! Thank you for your help, i really appreciate it :)!
    From gryzli

0 comments:

Post a Comment