我可以在Apacheconfiguration块中调用PHP脚本吗?

我使用PHP作为Apache模块,而不是CGI。 每个对特定虚拟主机的请求都应该导致PHP脚本执行('/srv/web/init.php'),我这样做:

<VirtualHost *:80> ServerName example.com DocumentRoot /srv/web/ RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !^/init\.php$ RewriteRule .* /init.php [L] </VirtualHost> 

除了执行init.php并将其输出返回给Apache以外,这个虚拟主机没有其他用途。 我的问题是,是否有一个更清洁的方式来做到这一点 – 即。 绕过Apache的文件系统仿真,以便不需要“公共”文件夹,所以mod_rewrite是没有必要的。 就像调用一个CGI脚本一样,它会这样:

 <VirtualHost *:80> ServerName example.com PHPScript /srv/web/init.php </VirtualHost> 

我已经删除了上面的DocumentRoot指令,因为我提出的将会使其无用。 我只是想把Apache模块变成CGI,而且我应该只使用CGI?

不,你不能直接调用一个PHP脚本。 你可以做的是这样的:

 <VirtualHost *:80> ServerName example.com DocumentRoot /srv/web/ DirectoryIndex init.php </VirtualHost> 

没有,

PHPScript不是您可以使用的Apache指令。 http://httpd.apache.org/docs/2.2/mod/directives.html

你必须使该文件的默认索引或mod_rewrite它。

您需要将该PHP文件作为默认索引文件,然后作为前端控制器运行到该网站上的所有其他位置,以便所有url都将是example.com/index.php/whatever – 您可以进行重写,以隐藏index.php部分给最终用户,但它仍然会在那里指挥的东西。