【发布时间】:2019-05-15 14:29:05
【问题描述】:
我从远程服务器拉取了一个 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 文件,public 中有index.php,我的public 文件夹中有以下.htaccess(从生产服务器复制):
<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 时,它会在“路由”下显示:
【问题讨论】:
-
你试过我的解决方案了吗?
-
检查您的 env 文件,我认为必须有一个您没有更改的应用程序 url。
-
你试过
php artisan storage:link吗?也许资产是从存储文件夹提供的,而不是资产目录本身。 -
@jovan 请提供 .env 文件
-
图片的绝对文件系统路径是什么?
DocumentRoot是什么?您的配置中缺少某些内容...如果您的应用程序安装在根目录(文档根目录?)之外的/public子目录中,并且 URL 路径中不包含/public子目录,那么您需要 将请求重写到/public子目录的东西 - 您发布的内容中没有任何内容可以做到这一点?