首页 > 解决方案 > 所有 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有以下内容(从生产服务器复制):.htaccesspublic

<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它在“路由”下显示:

在此处输入图像描述

标签: phplaravel.htaccess

解决方案


我有同样的问题,我修复了它。尝试这个。我更改了/vendor/Illuminate/Foundation/helpers.phpasset () 函数如下:

function asset($path, $secure = null)
{
    return app('url')->asset("public/".$path, $secure);
}

希望它会起作用


推荐阅读