【问题标题】:apache not serving static content correctlyapache没有正确提供静态内容
【发布时间】:2011-12-02 17:59:44
【问题描述】:

我一直在开发自己的 mvc 框架以进一步学习 Web 应用程序,但在提供静态资源时遇到了麻烦。我试图在应用程序中有一个入口点,也就是前端控制器,所以在我的项目/我有一个 .htaccess 文件,它将所有请求重定向到 app/ 文件夹,另一个 .htaccess 将请求 uri 传递给 index.php (在 app/ 中)谁将请求委托给适当的控制器。

但是,当我尝试提供静态内容(例如 javascript 或级联样式表)时,我仍然会通过 app/index.php 重定向。我还在 /var/log/apache2/errors.log 中收到“favicon.ico 不存在于 /var/www”错误(可能是因为符号链接到 ~/www?)。我不希望因为我的项目根目录中的以下 .htaccess 文件:

<IfModule mod_rewrite.c>
  Options -Indexes +FollowSymLinks
  RewriteEngine On

  RewriteBase /

  # ----------------------------------------------------------------------
  # Suppress the "www." at the beginning of URLs
  # ----------------------------------------------------------------------
  # The same content should never be available under two different URLs - especially not with and
  # without "www." at the beginning, since this can cause SEO problems (duplicate content).
  # That's why you should choose one of the alternatives and redirect the other one.
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  #----------------------------------------------------------------------
  # Route static resources to respective files
  #----------------------------------------------------------------------
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond public/$0 -f
  RewriteRule ^.+\.(jpg|gif|png|ico|css|js)$ /public/$0 [L]

  #----------------------------------------------------------------------
  # Redirect all other requests to the app folder
  #----------------------------------------------------------------------
  RewriteRule   ^$    app/   [L]
  RewriteRule   (.*)  app/$1 [L]
</IfModule>

这是我的 app/ 文件夹中的 .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # ensure request is not path to filename or directory
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d

  # redirect all requests to index.php?url=PATHNAME
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

为什么我不能正确地提供静态内容?如果我不尝试将所有静态请求发送到 public/,这对我来说是有意义的,这是我的 css、jpg、png、js 等文件所在的位置。但是我有一个 RewriteCond 规则,可以将这些文件的请求发送到公共目录......令人困惑?

【问题讨论】:

  • 我试过删除RewriteCond %{REQUEST_FILENAME} !-f,因为它会过滤所有文件,但仍然不起作用。

标签: php apache .htaccess


【解决方案1】:

据我了解,假设您的项目结构如下:

/ /.htaccess /app/.htaccess /app/index.php /public/static.js(例如)

这是我想出的,希望它能解决你的问题: 根文件夹中的 .htaccess:

<IfModule mod_rewrite.c>
    Options -Indexes +FollowSymLinks
    RewriteEngine On

    RewriteBase /

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    RewriteRule ^public/.+\.(jpg|gif|png|ico|css|js)$ - [L]
    RewriteRule ^(.*)$ app/$1 [L]
</IfModule>

并且app文件夹中的.htaccess不变。

每个以 public 开头并且是具有列出的扩展名的文件的请求都不会被重定向,这是用破折号字符完成的。 最后一条规则允许将请求重定向到 app/index.php 文件。

我认为结果行为是预期的:

  • 公共目录中的静态文件不会被重定向,
  • 公共目录中具有另一个扩展名的文件将被 重定向到 app/index.php(可能是为了一些错误处理),
  • 不以 public 开头的请求将被重定向到 应用程序/index.php。

【讨论】:

  • 是的,就我的目录结构和我想要完成的工作而言,这几乎是正确的。谢谢!我会测试一下并发布结果。
  • 还是不行。当我直接链接到文件时,我得到的是索引页面(因为这是我迄今为止唯一的操作)而不是文件。也许它应该是 RewriteCond ,RewriteCond ,RewriteRule 。这也可能消除 apache2 errors.log 中的“No favicon.ico in /var/www”消息...
  • 这很奇怪,因为我测试了我发布的内容。如果您使用的是 Apache 配置或您的虚拟主机,问题可能出在其他地方。
  • 我认为 Zend Framework 有关于项目结构的最佳方法之一,如果你想看看它。
  • 我会继续检查,谢谢。我不接受这一点,希望能够像你一样让它工作。
猜你喜欢
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2011-09-17
  • 2014-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多