所有css / img文件都路由到网站的首页

时间:2018-12-14 10:16:13

标签: php laravel .htaccess

我已经从远程服务器上提取了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 /”,也尝试使用绝对/相对路径,但没有任何效果。

编辑:在此同时,我尝试了多种编辑.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时,它在“路由”下显示:

enter image description here

5 个答案:

答案 0 :(得分:1)

我有一个相同的问题,我已解决。尝试这个。 我更改了 /vendor/Illuminate/Foundation/helpers.php asset()函数,如下所示:

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

希望它会起作用

答案 1 :(得分:0)

我真的不确定为什么它将在服务器上而不是在本地上运行,除非服务器上的某些设置/工具与您在本地上的不一样...也许是在CDN前面?这样可以防止请求甚至到达服务器并从服务中获取文件……反正,这就是我要解决的问题:

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

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

也有可能仅在目标文件存在的情况下进行重写,但就我而言,我更愿意发出404错误,而不是使用户登陆首页。

编辑:请注意,此答案使用的是我的全局htaccess知识。我不太了解Laravel,它可能会通过其PHP处理静态资产...但是,无论采用哪种框架,我的解决方案都应能正常工作。

答案 2 :(得分:0)

只需如下替换您的主目录.htaccess文件,然后尝试。它既可以在服务器中使用,也可以在本地使用。确保备份原始的htaccess文件。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

</IfModule> 

答案 3 :(得分:0)

您的服务器使用apache的自定义模块,例如deflate, gzip,但是这些服务器可能不在您的本地计算机上。

首先,您是否可以尝试在.htaccess文件中使用以下代码作为laravel的默认内容。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

如果以上代码正常运行,则可以尝试安装这些自定义模块。

答案 4 :(得分:0)

将以下代码添加到apache文件夹C:\ xampp \ apache \ conf \ extra \ httpd-vhosts.conf下的虚拟主机文件中。

<VirtualHost *:8005>
  ServerName my-app.local
  DocumentRoot "C:/XAMPP/htdocs/laravel/public"
  <Directory "C:/XAMPP/htdocs/laravel/public">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
  </Directory>
</VirtualHost>

注意:将laravel替换为您的project folder名称。

在此之后,请从C:\Windows\System32\drivers\etc\hosts编辑您的主机文件并在下面添加行

127.0.0.1 my-app.local

最后重新启动您的Apache,并在重新启动后尝试使用http://my-app.local

访问您的应用程序

注意:请不要忘记从HTACCESS文件中删除所有更改

希望它会起作用