[英]apache not serving static content correctly
我一直在研究自己的mvc框架,以进一步学习Web应用程序,但是在提供静态资源方面遇到了麻烦。 我试图在应用程序中有一个入口点,也就是前端控制器,所以在我的项目/我有一个.htaccess文件,将所有请求重定向到app /文件夹,其中另一个.htaccess将请求uri传递给index.php (在app /中)将请求委托给适当的控制器。
但是,当我尝试提供静态内容时,例如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>
这是我的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规则将这些文件的请求发送给公共目录......令人困惑?
根据我的理解,假设您的项目结构如下:
/ /.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>
app文件夹中的.htaccess保持不变。
每个以public开头并且是具有列出的扩展名的文件的请求都不会被重定向,这是使用短划线字符完成的。 最后一条规则允许将请求重定向到app / index.php文件。
我认为最终的行为是预期的行为:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.