2011-10-07 54 views
4

我一直在研究自己的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規則在那裏將這些文件的請求發送給公共目錄...令人困惑?

+0

我嘗試刪除'RewriteCond%{REQUEST_FILENAME}!-f',因爲它過濾所有文件,但仍然不起作用。 – Andrew

回答

1

假設,從我的理解,您的項目結構如下:

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

這裏是我想出的,希望它會解決你的問題: 在根文件夾中的.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> 

而應用程序文件夾中的.htaccess保持不變。

每個以公開開頭並且是具有列出的擴展名的文件的請求都不會被重定向,這是用破折號字符完成的。 最後一條規則允許將請求重定向到app/index.php文件。

我認爲,產生的行爲是預期的一個:

  • 公共目錄靜態文件不被重定向,
  • 文件在公共目錄中另一部分將被 重定向到應用程序/索引。 PHP的(也許是一些錯誤治療),
  • 請求不公開將被重定向到 app/index.php。
+0

是的,這在我的目錄結構以及我正在努力完成的事情方面非常有用。謝謝!我會在一些測試中發佈結果。 – Andrew

+0

仍然不起作用。當我直接鏈接到這些文件時,我被提供給索引頁面(因爲它是我迄今唯一的動作)而不是文件。也許它應該是一個RewriteCond ,RewriteCond <匹配這些擴展>,RewriteRule <到公用文件夾>。這可能也會消除apache2 errors.log中的「No favicon.ico in/var/www」消息... – Andrew

+0

這很奇怪,因爲我測試了我發佈的內容。也許這個問題在Apache配置或者你的虛擬主機中的其他地方,如果你使用的話。 – florian