基于所请求的资源是否存在于第一个中的Apacheconfiguration使用两个文档根

背景

我有一个由CakePHP安装和Magento安装组成的客户端站点:

/web/example.com/ /web/example.com/app/ <== CakePHP /web/example.com/app/webroot/ <== DocumentRoot /web/example.com/app/webroot/store/ <== Magento /web/example.com/config/ <== Site-wide config /web/example.com/vendors/ <== Site-wide libraries 

服务器运行Apache 2.2.3。

问题

整个公司都有FTP访问权限,并习惯于阻塞/web/example.com/app/webroot//web/example.com/app/webroot/store/目录自己的文件。 有时这些文件需要HTTP访问,有时候不需要。

无论如何,这种混乱让我的工作更难维护网站。 代码合并,tar实时代码等,是非常复杂的,通常需要一堆filter。

被遗弃的解决scheme

起初,我以为我会在同一台服务器上build立一个新的子域名,将其所有文件移动到那里,然后更改他们的FTP chroot。 但是,这不会因为这些原因:

首先,我不知道(他们也不记得)他们发送的哪些营销材料包含URL,这些材料包含他们上传到服务器的某些资源,使用主域,还使用了使用主要虚拟的抽象子域主机,因为它具有ServerAlias *.example.com 。 所以突然让他们只使用static.example.com是不可行的。

其次,他们的项目中的PHP脚本可能是非常不便携的。 我希望他们的文件保持在与我所能build立的环境类似的环境中。 此外,我不想debugging他们的代码,使其便携式。

一半的解决scheme

经过一番思考,我决定find一种方法,把实际的网站文件分割成另一个他们不会碰到的目录。 公司上传的文件将保持在原来的位置。 这将确保我没有破坏任何需要HTTP访问的项目。 它看起来像这样:

 /web/example.com/ <== A bunch of their files are in here /web/example.com/app/webroot/ <== 1st DocumentRoot; A bunch of their files are in here /web/example.com/app/webroot/store/ <== Some more are in here /web/example.com/site/ <== New dir; Contains only site files /web/example.com/site/app/ <== CakePHP /web/example.com/site/app/webroot/ <== 2nd DocumentRoot /web/example.com/site/app/webroot/store/ <== Magento /web/example.com/site/config/ <== Site-wide config /web/example.com/site/vendors/ <== Site-wide libraries 

在我做了这个改动之后,除了/web/example.com/site/内的东西外,我不需要关注任何事情,而且我的工作会更容易。 我将是唯一一个在那里改变的东西。

所以,这就是Apache魔法的发生地点:我需要一个HTTP请求来http://www.example.com/首先使用/web/example.com/app/webroot/作为文档根目录。 如果没有发现任何东西(没有find其他上传的公司项目),请尝试在/web/example.com/site/app/webroot/find某些/web/example.com/site/app/webroot/

另外要记住的是,如果$_SERVER['DOCUMENT_ROOT']variables读取/web/example.com/app/webroot/但是实际文件位于/web/example.com/site/app/webroot/ 。 对于/web/example.com/site/app/webroot/目录中的任何内容,DOCUMENT_ROOT环境variables可以是/web/example.com/site/app/webroot/

结论

Apache 2.2.3可能是我的一半解决scheme吗?

有没有更好的方法来解决这个问题?

我相信mod_rewrite会做的伎俩。 在您的虚拟主机www.example.com,你可以做这样的事情:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /site/app/webroot/$1 [R=301,L] 

这应该用301请求重写任何不存在的文件/目录请求到site / app / webroot / *。 这应该更新DOCUMENT_ROOT以匹配目录。 您可以删除R=301标志只是做一个内部重写(用户没有看到变化),但我不是100%肯定会更新DOCUMENT_ROOT …它可能会。

请注意,这个例子是从内存和未经testing,但它给出了一个mod_rewrite可以做什么的一般概念