.htaccess - 如果找到,则在 .phar 文件上提供请求,否则在 index.php 上提供服务

.htaccess - serve requests on a .phar file if found, otherwise serve on index.php

我想部署一个打包在 .phar 文件中的 Web 应用程序,并像我在 $_SERVER["DOCUMENT_ROOT"].

中的 index.php 文件中实际做的那样处理来自它的请求

Apache 中的默认 mod_rewrite 是这样的:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

如果我想对 .phar 文件做同样的事情,我想(虽然我可能是错的,所以任何方向都非常受欢迎)我可以这样做:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /application.phar [L]
</IfModule>

我总是可以有两个 .htaccess 文件,并根据部署类型使用适当的文件,但我希望尽可能支持这两种策略。无论我想 运行 应用程序打包还是解包,我都希望能够在项目之间重复使用 .htaccess 文件,但考虑到打包版本应该是首选方式。

如何将这两个不同的规则合并为一个?

像这样:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    # ignore all existing files/directories from rewrite
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # if application.phar exists then use it
    RewriteCond %{DOCUMENT_ROOT}/application.phar -f
    RewriteRule ^ application.phar [L]

    # otherwise use index.php
    RewriteRule ^ index.php [L]

</IfModule>