apache不能正确提供静态内容 [英] apache not serving static content correctly

查看:92
本文介绍了apache不能正确提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working on my own mvc framework to further my web app learning, but am having trouble serving static resources. I am trying to have a single entry point into the application, aka a front controller, so in my project / I have an .htaccess file that redirects all requests to the app/ folder where another .htaccess passes the request uri to index.php (in app/) who delegates the request to the appropriate controllers.

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

However, when I try to serve up static content, such as javascripts or cascading style sheets, I still get redirected through app/index.php. I am also getting "favicon.ico does not exist in /var/www" errors in /var/log/apache2/errors.log (maybe because of symlink to ~/www?). I do not expect to because of the following .htaccess file in the root directory of my project root:

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

and here is the .htaccess in my app/ folder:

<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规则,可以将对此类文件的请求发送到公共目录...令人困惑吗?

Why can't I serve static content correctly? This would make sense to me if I wasn't trying to sent all static requests to public/, which is where my css, jpg, png, js, etc files reside. But I have a RewriteCond rule in there to send the requests for such files to the public dir... Confusing?

推荐答案

据我所知,您的项目结构如下:

Assuming, from what I understood, that your project structure is the following:


/
/.htaccess
/app/.htaccess
/app/index.php
/public/static.js (for example)

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

Here is what I come up with, hoping it'll solve your problem: the .htaccess in the root folder:

<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保持不变.

And the .htaccess in the app folder is unchanged.

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

Every request starting with public and being a file with the listed extensions won't be redirected which is done with the dash character. The last rule allows to redirect a request to the app/index.php file.

我认为最终的行为是预期的行为:

I think the resulting behaviour is the expected one:

  • 公共目录中的静态文件未重定向
  • 在公共目录中具有另一个扩展名的文件将是 重定向到app/index.php(可能有一些错误处理),
  • 不是以public开头的请求将被重定向到 app/index.php.
  • static files in the public directory are not redirected,
  • files with another extension in the public directory will be redirected to app/index.php (maybe for some error treatment),
  • requests not starting with public will be redirected to app/index.php.

这篇关于apache不能正确提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆