简体   繁体   English

Laravel 5 - URL重写和CSS冲突

[英]Laravel 5 - URL Rewriting and CSS conflict

I have a problem in Laravel 5 between my .htaccess for the URL rewriting and my CSS files. 我的.htaccess之间的Laravel 5在URL重写和我的CSS文件之间存在问题。 First of all, here is my project tree: 首先,这是我的项目树:

我的项目

I have a .htaccess in my public folder as you can see on the picture. 我在公共文件夹中有一个.htaccess,如图所示。 Here is the content of my .htaccess: 这是我的.htaccess的内容:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    RewriteRule ^ index.php [L]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>

This is the code I use to remove "index.php" in URLs. 这是我用来删除URL中“index.php”的代码。 For example, http://localhost/mySite/public/index.php/about becomes http://localhost/mySite/public/about . 例如, http://localhost/mySite/public/index.php/about变为http://localhost/mySite/public/about

Now, I have a master page (in /resources/views/ ) and in this master page I would like to reference the css file which is located in /public/css/style.css . 现在,我有一个母版页(在/resources/views/ ),在这个母版页中,我想引用位于/public/css/style.css的css文件。 I added this code in my master page: 我在我的母版页中添加了这段代码:

<link rel='stylesheet' href="{{ URL::asset('css/style.css') }}" >

This does not work with the current htaccess but if I comment the line RewriteRule ^ index.php [L] in my htaccess and access to the URL (with "index.php") the stylesheet is well referenced. 这不适用于当前的htaccess,但如果我在我的htaccess中注释RewriteRule ^ index.php [L]行并访问URL(带有“index.php”),则可以很好地引用样式表。 I think I have to add an other line in htaccess but I have no idea what I have to add. 我想我必须在htaccess中添加另一行,但我不知道我要添加什么。

I hope you can help me and don't hesitate to ask for more details. 我希望你能帮助我,不要犹豫,询问更多细节。 Thanks. 谢谢。

Why did you change the default .htaccess ? 你为什么要改变默认的.htaccess

It should be like this: 它应该是这样的:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

The two RewriteCond prevent requests for files to be routed to Laravel. 两个RewriteCond阻止将文件请求路由到Laravel。 But they have to be before the RewriteRule to be effective. 但他们必须在RewriteRule 之前才能有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM