加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – apache没有正确提供静态内容

发布时间:2020-12-13 22:32:11 所属栏目:PHP教程 来源:网络整理
导读:我一直在研究自己的mvc框架,以进一步学习Web应用程序,但是在提供静态资源方面遇到了麻烦.我试图在应用程序中有一个入口点,也就是前端控制器,所以在我的项目/我有一个.htaccess文件,将所有请求重定向到app /文件夹,其中另一个.htaccess将请求uri传递给index.p
我一直在研究自己的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文件.

我认为最终的行为是预期的行为:

>不重定向公共目录中的静态文件,>将在公共目录中包含另一个扩展名的文件重定向到app / index.php(可能是一些错误处理),>不以public开头的请求将被重定向到应用程序/ index.php文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读