静态树内的动态路由


Dynamic route inside a static tree

我有一个静态文件的目录结构,我想与我的Web服务器一起提供。说:

project/
    directory1/
    directory2/
    directory3/
        subdirectory1/
        subdirectory2/
        subdirectory3/
            static.html
            static.png
            static.js
            static.css
...

没有什么神奇的,在这里,我们可以使用一个简单的虚拟主机。

<VirtualHost *:80>
    ServerName project.dev
    DocumentRoot "path/to/project"
    <Directory "path/to/project">
        Require all granted
    </Directory>
</VirtualHost>

现在假设我们想拦截GET http://project.dev/directory3/subdirectory3/static.html做一些脚本并提供我们想要的东西。

问题:我们如何配置我们的 Apache vhost 以使用 php、python、ruby 或任何语言来解释此请求并返回一些响应而不是实际的静态文件?

像这样:

#...
<Directory "path/to/project">
    Require all granted
</Directory>
<Directory "path/to/project/directory3/subdirectory3/static.html">
    send_to_some_framework "path/to/project/app.php"
</Directory>
#...

我会使用mod_rewrite。像这样:

RewriteRule ^/directory3/subdirectory3/static.html$ path/to/project/app.php [L]

或带AliasMatch

AliasMatch "^/(.*/static'.html)$" "path/to/project/app.php$1"