【问题标题】:.htaccess - serve requests on a .phar file if found, otherwise serve on index.php.htaccess - 如果找到,则在 .phar 文件上提供请求,否则在 index.php 上提供
【发布时间】:2019-05-01 17:02:38
【问题描述】:

我想部署一个打包在 .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 文件,但考虑到打包版本应该是首选方式。

如何将这两种不同的规则合二为一?

【问题讨论】:

    标签: .htaccess phar


    【解决方案1】:

    像这样:

    <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>
    

    【讨论】:

    • 非常感谢。我仍然缺乏对 .phar 文件的一些测试,但 .htaccess 规则就像一个魅力:)
    猜你喜欢
    • 2019-11-28
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2020-09-07
    • 2017-06-24
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多