Apache dynamic alias based on sub-domain
Solution 1:
[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.
Solution 2:
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>