Apache——“动态”重写规则

发布于 2024-09-04 10:56:47 字数 1315 浏览 2 评论 0原文

我正在开发一个 Zend Framework 项目,在该项目中我偶然发现了一些问题。 问题源于这样一个事实:模块是 Zend Framework 中的二等公民。在我的项目中,我希望每个模块都有一个文件夹,其中包含可从外部访问的文件 - 例如样式表、javascript 和图像等文件。

现在,这该怎么办呢?对于 Zend Framework 项目,我有一个如下所示的文件夹结构:

  • application/
    • 模块/
      • 模块一/
        • 公开/
          • 样式表.css
      • 模块二/
      • 模块三/
  • public/
    • index.php

位于 public/ 文件夹中的标准 .htaccess 文件包含以下内容:

SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

它的工作方式是,项目的 apache DocumentRoot 是 public/文件夹。所有请求都通过 Zend Framework 的路由器组件接管的 index.php 文件进行重定向。现在,我绝不是 Apache 或 mod_rewrite 方面的专家,所以请原谅我,如果这只是愚蠢的。我想我在现有的重写规则中实现了一个额外的步骤,这样如果我请求 http:// /project/public/moduleOne/stylesheet.css 它将解析为 /var/www/project/application/modules/moduleOne/public/stylesheet.css

因此,需要完成的步骤是检查 URI 中的第一个元素是否为 public/ 如果是,我们将下一段作为模块名称,并在我们的路径中使用它尝试解析并尝试提供该文件。

这是否可能,或者有人有更好的建议吗?

谢谢您的宝贵时间 克里斯蒂安·拉斯穆森

I'm working on a Zend Framework project where I've stumbled across a bit of a problem.
The problem originates from the fact that modules are 2nd class citizens in Zend Framework. In my project, I'd like for each module to have a folder containing files which are to be accessed from the outside - files such as stylesheets, javascripts and images.

Now, how is this to be done. With a Zend Framework project I have a folder structure which looks like this:

  • application/
    • modules/
      • moduleOne/
        • public/
          • stylesheet.css
      • moduleTwo/
      • moduleThree/
  • public/
    • index.php

The standard .htaccess file located in the public/ folder holds this:

SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

The way it works, is that the project's apache DocumentRoot is the public/ folder. All requests gets redirected through the index.php file where Zend Framework's router component takes over. Now, I'm by no means an expert with Apache nor mod_rewrite so pardon me if this is just silly. I imagine that I implement an extra step in the existing rewrite rule so that if I request http://project/public/moduleOne/stylesheet.css it will for instance resolve to /var/www/project/application/modules/moduleOne/public/stylesheet.css.

So the steps which need to be done is to check if the first element in the URI is public/ if it is, we take the next segment as the modules name and use that in the path we're trying to resolve to and attempt to serve the file.

Is this at all possible or does anyone have a better suggestion?

Thank you for your time
Christian Rasmussen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-09-11 10:56:47

仅使用 mod_rewrite 您无法请求文档根目录之外的文件。因此,唯一的解决方案是使用位于文档根目录并指向模块目录的符号链接。

With just mod_rewrite you can't request files outside the document root. So the only solution is to use symlink located in document root directory and pointing to the modules directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文