6

I have the following structure as part of a ZF1 application -

C:/www/multisite
C:/www/multisite/application
C:/www/multisite/public
C:/www/multisite/client1/uploads
C:/www/multisite/client2/uploads

I currently have the following vhost for each sub-domain -

<VirtualHost *:80>
    DocumentRoot "C:/www/multisite/public"
    ServerName client1.multisite.dev
    Alias /uploads C:/www/multisite/client1/uploads
    FallbackResource /index.php
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "C:/www/multisite/public"
    ServerName client2.multisite.dev
    Alias /uploads C:/www/multisite/client2/uploads
    FallbackResource /index.php
</VirtualHost>

I would like to achieve this dynamically based on the sub-domain but I cannot figure out how to do it.

{sub_domain}.multisite.dev

would lead to an alias of

Alias /uploads C:/www/multisite/{sub_domain}/uploads

Is this possible?

2

2 Answers 2

6
+250

[EDIT] As I have tested the solution. It now works like a dream as detailed below. I had made a silly slip-up in my regex which is now corrected.

The idea is to use RewriteCond in order to capture the fqdn in the request and split it up. The regex captures the leftmost part of the domainname which is then backreferenced in the RewriteRule using %1. I also escape the 'public' directory from translation, which makes it accessible using a URI path common to all fqdn:s mapped to the site (see test result for concrete example).

  • I have disabled the virtual hosts file for the purpose of this experiment.
  • My dns points client1.multisite.dev and client2.multisite.dev to my Apache 2.2 test server.
  • The following snippet is in my httpd.conf.

.

RewriteEngine On

RewriteMap lowercase int:tolower
RewriteCond %{REQUEST_URI}        !^/public/
RewriteCond ${lowercase:%{HTTP_HOST}}        ^([^.]+)\.multisite\.dev$
RewriteRule ^/(.*) /myApache/runtime/docs/www/multisite/%1/$1

I have created this directory structure.

/myApache/runtime/docs/www/multisite/client1/uploads
/myApache/runtime/docs/www/multisite/client2/uploads
/myApache/runtime/docs/www/multisite/public

Each dir has a separate file called index.htm. The index.htm content reveals the directory path where it is placed.

Surfing to http://client1.multisite.dev/public/index.htm OR to http://client2.multisite.dev/public/index.htm gives this result:

Hi!
Served from /myApache/runtime/docs/www/multisite/public/index.htm 
See you!

Surfing to http://client1.multisite.dev/uploads/index.htm gives this result:

Hi!
Served from /myApache/runtime/docs/www/multisite/client1/uploads/index.htm 
See you!

Surfing to http://client2.multisite.dev/uploads/index.htm gives this result:

Hi!
Served from /myApache/runtime/docs/www/multisite/client2/uploads/index.htm 
See you!

I also tested mixing cases when entering the URL in my web browser. Everything gets translated to lower case.

I note that you are running Apache on Windows whose native file systems are case insensitive. Normalizing case would therefore not be a requirement, though I personally find lowercase URL:s more aesthetically pleasing to look at in my browser. On file systems which are case sensitive normalization would be a requirement though, so I include it for completion.

Finally, I also took note that running Apache on a port other than 80 (i.e. having to spell it out in the url) breaks the RewriteRule backreference. I did not pursue a solution for that however.

I hope this is equal to or close enough to what you need.

3
  • Thank you. I do not know why I had not used this approach in the first place. For some reason I was stuck on using alias. I have not tried this but it is definitely the simple solution. Dec 12, 2013 at 11:31
  • I feel bad for not having time to test this due to tons of work though (for instance I note that I missed escaping the dots in the rewritecond regex), but until I can there are lots of resources with examples and variations to google. For instance: httpd.apache.org/docs/2.4/rewrite/vhosts.html and vicvijayakumar.com/2013/01/16/mass-virtual-hosting-in-apache and so on.
    – ErikE
    Dec 12, 2013 at 12:42
  • @nnichols Have you tried this yet? How did it go? Dec 13, 2013 at 19:42
4

Try mod_vhost_alias: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html Example:

<VirtualHost *:80>
    ServerName apps.mydomain.ltd
    ServerAlias *.apps.mydomain.ltd
    VirtualDocumentRoot /web/mydomain.ltd/apps/%1/public_html
    CustomLog /web/mydomain.ltd/access_apache.log vhost_log_format
    ErrorLog /web/mydomain.ltd/logs/error_apache.log
    ServerAdmin [email protected]    
    <Directory "/web/mydomain.ltd/apps">
        Options FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
    </Directory>
</VirtualHost>
1
  • 1
    Thank you for taking the time to make your suggestion but this would not solve my problem. It allows for VirtualScriptAlias but not VirtualAlias. Dec 8, 2013 at 21:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .