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 am trying to convert:

http://test.example.com/file.php

To either

http://www.example.com/file.php?subdomain=test

or

http://test.example.com/file.php?subdomain=test

Depending on which is simpler to do or faster

This works for the index page but for subfolders and files it gets in a redirect loop.

server {
      listen   80;

      # Make site accessible from http://localhost/
      server_name   ~^[^.]+.example.com$;
      rewrite ^/(.*)/$ /$1 permanent;

      if ($host ~* ^([^.]+).example.com$) {
              set $subdomain $1;
      }
      rewrite ^(.*)$ $1?subdomain=$subdomain last;

      location / {
              root /var/www/example.com;
              index index.html index.php;
      }

      location ~ \.php$ {
              try_files $uri =404;
              root            /var/www/example.com;
              fastcgi_pass    unix:/var/run/php-fpm.sock;
              fastcgi_index   index.php;
              fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script
              include         fastcgi_params;
     }
}
share|improve this question
add comment

1 Answer

I am not quite sure what your objective is - since your example shows two possibilities. The config below (untested) should result in:

http://test.example.com/file.php ==> http://www.example.com/file.php?subdomain=test

It should match any subdomain, and any file name.

server{
    server_name www.example.com;
    root /var/www/example.com;
    index index.html index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass    unix:/var/run/php-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script
        include         fastcgi_params;
    }
}
server {
    server_name  "~^(?<subdomain>.+)\.example\.com$";
    rewrite ^/(.*?)/?$ http://www.example.com/$1?subdomain=$subdomain;
}

Brief explanations:

  • static server_names are matched before regexes - so, any request to www.example.com will be handled by the first server block
  • root and index directives should be placed under the server block (not location block), if possible
  • the listen directive is not needed for port 80
  • the second server block uses a named capture to assign the subdomain to a variable
  • the rewrite captures everything from the first slash to the last, excluding both (the (.*?) is lazy).

(As an aside, I am really not quite sure what your config is supposed to do with cases like a directory or a static file. Currently, the following should occur (which doesn't seem sensible):

http://test.example.com/file.jpg ==> http://www.example.com/file.jpg?subdomain=test
http://test.example.com/path/to/subfolder/ ==> http://www.example.com/path/to/subfolder?subdomain=test

It looks like your current config does the same. Add a few more examples of what you want and I may be able to update this config to be more relevant).

share|improve this answer
    
Static files aren't a problem because they are being served from a cdn domain. The folders wouldn't the following happen: test.example.com/path/to/subfolder ==> example.com/path/to/subfolder?subdomain=test ==> example.com/path/to/subfolder/index.php?subdomain=test. I Think I'll be able to figure it out from what you have, I'll edit my question and mark you as the answer if I do. –  jpiasetz Mar 20 '12 at 18:54
    
For the folders, I am not sure that the arguments will be kept after using the index file - so, while you might end up with example.com/path/to/subfolder/index.php, you may have to manually add the arguments (with $args). As for one approach being 'easier' than the other, keeping the same subdomain is probably a bit faster (since you have only an internal rewrite of the arguments) - but it seems to have less purpose (i.e. you could parse the hostname in PHP to get the same info) - which is why I did the redirect to main domain approach (that and it might be better from an SEO perspective). –  cyberx86 Mar 20 '12 at 19:07
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.