2011-10-07 31 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