As someone who spends most of the day testing and developing eZ Publish, I always have multiple instances of eZ Publish checked out. Different versions, customer installations, etc. Previously I’ve created a new virtual host in Apache for each installation — very painful indeed.
I went searching for a better solution — one that wouldn’t need
reconfiguring of Apache. I could have used rewrite rules to map part of the URL to a directory, however rewrite rules will get messy
fast. I want my configuration to be as clean and simple as possible. Some
researching later and I stumbled upon the VirtualDocumentRoot
directive. From the VirtualDocumentRoot documentation:
The VirtualDocumentRoot directive allows you to determine where Apache will find your documents based on the value of the server name. The result of expanding interpolated-directory is used as the root of the document tree in a similar manner to the DocumentRoot directive’s argument.
Sounds perfect. Here’s my new and improved virtual host configuration I came up
with using VirtualDocumentRoot
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<VirtualHost *:80> <Directory /www> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> VirtualDocumentRoot /www/%1 RewriteEngine On RewriteRule content/treemenu/?$ /index_treemenu.php [L] Rewriterule ^/var/storage/.* - [L] Rewriterule ^/var/[^/]+/storage/.* - [L] RewriteRule ^/var/cache/texttoimage/.* - [L] RewriteRule ^/var/[^/]+/cache/texttoimage/.* - [L] Rewriterule ^/design/[^/]+/(stylesheets|images|javascript)/.* - [L] Rewriterule ^/share/icons/.* - [L] Rewriterule ^/extension/[^/]+/design/[^/]+/(stylesheets|images|javascripts?)/.* - [L] Rewriterule ^/packages/styles/.+/(stylesheets|images|javascript)/[^/]+/.* - [L] RewriteRule ^/packages/styles/.+/thumbnail/.* - [L] RewriteRule ^/favicon\.ico - [L] RewriteRule ^/robots\.txt - [L] RewriteRule .* /index.php [PT] </VirtualHost> |
How it works
This is the same virtual host setup as explained in the eZ Publish installation documentation, except for 2 new directives.
-
VirtualDocumentRoot /www/%1
Instead of
DocumentRoot
, we useVirtualDocumentRoot
to dynamically set the document root depending on the Host HTTP header. The%1
specifies that we only want the first part of the host name. For more information aboutVirtualDocumentRoot
, check out the mod_vhost_alias documentation. -
RewriteRule .* /index.php [PT]
[PT]
(pass through to next handler) was added to make sure the document root is set usingVirtualDocumentRoot
. If omitted the defaultDocumentRoot
setting will be used, resulting in paths looking like /www/index.php, instead of /www/trunk/index.php.
Examples
To illustrate how the mapping between the host header and directories on your file system, here are some examples:
Host | Document root |
---|---|
http://trunk | /www/trunk |
http://trunk.oh.ez.no | /www/trunk |
http://stable.oh.ez.no | /www/stable |
#1 Damien Pitard
Posted: June 24, 2008
Thanks for this usefull tips !!!
#2 Maxime THOMAS
Posted: June 24, 2008
Very useful, thanks for this… No more apache config days…
#3 Damien Pitard
Posted: June 25, 2008
mod_vhost_alias need to be enabled :
With apache 1.3 on debian, put the following line in /etc/apache/modules.conf LoadModule vhost_alias_module /usr/lib/apache/1.3/mod_vhost_alias.so
If you want to use url like http://yourlogin.thedomain.com with the eZPublish sources in /home/[HTML_REMOVED]/www/thedomain.com
Use the folowing directive for VirtualDocumentRoot : *VirtualDocumentRoot /home/%-3/www/%-1.0.%-2.0
Subdomains like http://admin.yourlogin.thedomain.com will also work with this directive (usefull if you use siteaccess mapped on subdomain).
#4 Damien Pitard
Posted: June 25, 2008
What’s this [HTML_REMOVED] tag ??? :(
you should read : If you want to use url like http://yourlogin.thedomain.com with the eZPublish sources in /home/yourlogin/www/thedomain.com”