php - 所有 css/img 文件都路由到网站的主页
问题描述
我从远程服务器中提取了一个 Laravel 应用程序并尝试设置本地版本。
我复制了所有文件和数据库,并设法为本地应用程序提供服务。所有的路线似乎都可以工作,除了一个大问题——从public
文件夹路线到网站主页的所有资产!
例如,我有一个包含这样的图像:
<img src="{{ asset('img/main-logo.png') }}" />
在生产服务器上,这会显示带有源 URL 的图像,https://www.example.com/img/main-logo.png
并且可以正常工作。
在我的本地服务器上,图像源结果是http://localhost:8005/img/main-logo.png
,这应该是正确的,但它没有显示图像。当我尝试在浏览器中打开此 URL 时,它没有显示图像,而是打开了网站主页!
我在根文件夹中有 server.php 文件,index.php
在我的文件夹中public
有以下内容(从生产服务器复制):.htaccess
public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
# Force compression for mangled headers.
# https://developer.yahoo.com/blogs/ydn/pushing-beyond-gzipping-25601.html
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_headers.c>
<filesMatch "\.(jpg|jpeg|png|gif|webp|js|ico)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>
</IfModule>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Map certain file types to the specified encoding type in order to
# make Apache serve them with the appropriate `Content-Encoding` HTTP
# response header (this will NOT make Apache compress them!).
# If the following file types wouldn't be served without the appropriate
# `Content-Enable` HTTP response header, client applications (e.g.:
# browsers) wouldn't know that they first need to uncompress the response,
# and thus, wouldn't be able to understand the content.
# http://httpd.apache.org/docs/current/mod/mod_mime.html#addencoding
<IfModule mod_mime.c>
AddEncoding gzip svgz
</IfModule>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE "application/atom+xml" \
"application/javascript" \
"application/json" \
"application/ld+json" \
"application/manifest+json" \
"application/rdf+xml" \
"application/rss+xml" \
"application/schema+json" \
"application/vnd.geo+json" \
"application/vnd.ms-fontobject" \
"application/x-font-ttf" \
"application/x-web-app-manifest+json" \
"application/xhtml+xml" \
"application/xml" \
"font/opentype" \
"image/svg+xml" \
"image/x-icon" \
"text/cache-manifest" \
"text/css" \
"text/html" \
"text/javascript" \
"text/plain" \
"text/vtt" \
"text/x-component" \
"text/xml"
</IfModule>
RewriteEngine On
RewriteRule ^robots\.txt$ robots.php
# rewrite non www to www
RewriteCond %{HTTP_HOST} ^localhost:8005 [NC]
RewriteRule ^(.*)$ http://localhost:8005/$1 [L,R=301,NC]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# redir for last / in url
RewriteCond %{REQUEST_METHOD} =GET
RewriteRule ^(.*)\/(\?.*)?$ /$1$2 [NC,L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
所以它是相同的 .htaccess,相同的路由,但在生产服务器上加载的资产不会在本地加载。任何人都知道为什么会这样以及如何解决它?
我尝试将“public/”添加到资产 URL,也尝试了绝对/相对路径,但没有任何效果。
编辑:与此同时,我尝试以各种方式编辑 .htaccess,甚至完全删除它,网站处理公共资产路由的方式没有任何改变。
编辑#2:http://localhost:8005/img/main-logo.png
加载网站主页时,http://localhost:8005/public/img/main-logo.png
出现 404 错误
编辑#3:当我加载http://localhost:8005/img/main-logo.png
它在“路由”下显示:
解决方案
我有同样的问题,我修复了它。尝试这个。我更改了/vendor/Illuminate/Foundation/helpers.phpasset () 函数如下:
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
希望它会起作用
推荐阅读
- python - 如何在我的 tkinter 文本窗口中打印结果?
- excel - 用户定义的函数在不同的选项卡上返回相同的结果
- java - 从服务传递到组件的角度错误消息
- c++ - R 或 C++ 中的半空间联合和交集
- javascript - Facebook 的 Spark AR Studio 中的垂直平面跟踪
- ios - 按钮中的 Swift 按钮
- mysql - 我想测试一个活动是否还有地方,如果是,用户可以参与,我正在使用 node js api rest
- python - 如何在 PySide2 中显示图像?
- java - android中的HTML子弹标签支持
- rest - 实时通知和结果接收