【发布时间】: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,因为它会过滤所有文件,但仍然不起作用。