apache没有正确提供静态内容

时间:2022-05-16 11:20:23

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.

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

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:

但是,当我尝试提供静态内容时,例如javascripts或级联样式表,我仍然可以通过app / index.php重定向。我在/var/log/apache2/errors.log中也得到“/ em / www中不存在”favicon.ico“错误(也许是因为符号链接到〜/ 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>

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

这是我的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>

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?

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

1 个解决方案

#1


1  

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

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

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

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

这是我想出来的,希望它能解决你的问题:根文件夹中的.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>

And the .htaccess in the app folder is unchanged.

app文件夹中的.htaccess保持不变。

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.

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

I think the resulting behaviour is the expected one:

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

  • 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),
  • 公共目录中具有另一个扩展名的文件将被重定向到app / index.php(可能用于某些错误处理),

  • requests not starting with public will be redirected to app/index.php.
  • 不以public开头的请求将被重定向到app / index.php。

#1


1  

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

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

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

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

这是我想出来的,希望它能解决你的问题:根文件夹中的.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>

And the .htaccess in the app folder is unchanged.

app文件夹中的.htaccess保持不变。

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.

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

I think the resulting behaviour is the expected one:

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

  • 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),
  • 公共目录中具有另一个扩展名的文件将被重定向到app / index.php(可能用于某些错误处理),

  • requests not starting with public will be redirected to app/index.php.
  • 不以public开头的请求将被重定向到app / index.php。