我一直在研究自己的mvc框架以進一步學習我的Web應用程序,但在服務靜態資源時遇到問題。我試圖在應用程序中有一個入口點,也就是前端控制器,所以在我的項目/我有一個.htaccess文件,將所有請求重定向到應用程序/文件夾,其中另一個.htaccess將請求URI傳遞給index.php (在app /中)將請求委託給適當的控制器。apache不能正確提供靜態內容
但是,當我嘗試提供靜態內容(如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>
而這裏的.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',因爲它過濾所有文件,但仍然不起作用。 – Andrew