所有css/img文件都会路由到该站点的主页

jov*_*van 6 php .htaccess laravel

我从远程服务器上拉了一个Laravel应用程序并尝试设置本地版本.

我复制了所有文件和数据库,并设法为本地应用程序提供服务.所有路线似乎都有效,除了一个大问题 - public文件夹中的所有资产都路由到网站的主页!

例如,我有一个像这样包含的图像:

<img src="{{ asset('img/main-logo.png') }}" />
Run Code Online (Sandbox Code Playgroud)

在生产服务器上,这将显示带有源URL的图像,https://www.example.com/img/main-logo.png并且此操作正常.

在我的本地服务器上,图像源结果是http://localhost:8005/img/main-logo.png,这应该是正确的,但它不显示图像.当我尝试在浏览器中打开此URL时,它不会显示图像,而是打开网站主页!

我在根文件夹中的文件server.php,index.phppublic和下面.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>
Run Code Online (Sandbox Code Playgroud)

所以它是相同的.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它时,在"路由"下显示:

在此输入图像描述

Sal*_*ter 0

我真的不确定为什么它会在服务器上工作而不是在本地工作,除非服务器上的一些设置/工具与本地的不一样......也许它在 CDN 前面?这将阻止请求甚至访问服务器并从服务获取文件......无论如何,这是我解决这个问题的方法:

RewriteRule     ^img/(.*) path/to/public/$1 [L]
# Handle Front Controller...
# ...
Run Code Online (Sandbox Code Playgroud)

添加的这一行会将所有以 img 开头的内容重写到您的公共目录中,这样文件将直接得到服务。您需要更改规则以适应任何其他静态资源,就像您可能需要为您的 javascript 和 css 文件执行此操作一样?

也可以仅在目标文件存在时重写,但就我而言,我更喜欢给出 404 错误,而不是让用户登录到主页。

编辑:请注意,这个答案使用了我的全局 htaccess 知识。我对 Laravel 不太了解,它可能会处理 PHP 中的静态资源...但无论框架如何,我的解决方案都应该有效。