Laravel 5 not finding css files

Php.HtaccessLaravelLaravel 5

Php Problem Overview


I've just installed a Laravel 5 project on MAMP and my pages are not finding the css files.

This is the link to my css in my app.blade.php file:

<link href="/css/app.css" rel="stylesheet">

And my .htaccess file has this line:

RewriteBase /laravel-site/laravel-5-app/public/

In the config folder my app.php file contains this:

'url' => 'http://localhost:8888/laravel-site/laravel-5-app/public/',

But when I open up this page: http://localhost:8888/laravel-site/laravel-5-app/public/auth/login and check the developer tools, it is looking for the css file at this location: http://localhost:8888/css/app.css

Just as a side note, if I go to this url: http://localhost:8888/laravel-site/laravel-5-app/public/ I get the correct welcome page.

Php Solutions


Solution 1 - Php

Use this to add assets like css, javascript, images.. into blade file.

FOR CSS,

<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >

OR

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

FOR JS,

<script type="text/javascript" src="{{ asset('js/custom.js') }}"></script>

OR

 <script type="text/javascript" src="{{ URL::asset('js/custom.js') }}"></script>

FOR IMAGES,

{{ asset('img/photo.jpg'); }}

Here is the DOC

Alternatively, if you pulled the composer package illuminate/html which was come as default in laravel 4.2 then you can use like below, In laravel 5. you have to manually pull the package.

{{ HTML::style('css/style.css') }}

Here is an Example.

Solution 2 - Php

In the root path create a .htaccess file with

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/ 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f



RewriteRule ^(.*)$ /public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

In public directory create a .htaccess file

<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>

if you have done any changes in public/index.php file correct them with the right path then your site will be live.

what will solve with this?

  • Hosting issue of laravel project.
  • Css not work on laravel.
  • Laravel site not work.
  • laravel site load with domain/public/index.php
  • laravel project does not redirect correctly

Solution 3 - Php

Stop artisan serve

and try using

php -S localhost:8000 -t public

Solution 4 - Php

You can use one of the following options:

<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >

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

{!! Html::style( asset('css/app.css')) !!}

Solution 5 - Php

It's works. For css:

<link href="css/app.css" rel="stylesheet">

Try to:

 <link href="public/css/app.css" rel="stylesheet">

Solution 6 - Php

To fast apply simple regex.

search : href="(.+?)"
replace : href="{{ asset('$1') }}"

and

search : src="(.+?)"
replace : src="{{ asset('$1') }}"

Solution 7 - Php

Here is the solution

  {!! Html::style('css/select2.min.css') !!}
  {!! Html::script('js/select2.min.js') !!}

Solution 8 - Php

This works for me

If you migrated .htaccess file from public directory to project folder and altered server.php to index.php in project folder then this should works for you.

<link rel="stylesheet" href="{{ asset('public/css/app.css') }}" type="text/css">

Thanks

Solution 9 - Php

I was having the same problem, until just now.

Removing the / from before /css/app.css so that its css.app.css worked for me.

Solution 10 - Php

I used:

"{{asset('css/custom.css')}}"
"{{asset('css/app.css') }}"

First one is my own file the second one came with laravel. I'm using version 5.

http://spanibox.com/css/app.css works but http://spanibox.com/css/custom.css does not. I find this very weird.

Solution 11 - Php

If you are using laravel 5 or 6:

  1. Inside Public folder create .css, .js, images... folders

enter image description here

  1. Then inside your blade file you can display an images using this code :

<link rel="stylesheet" href="/css/bootstrap.min.css">
<link src="/images/test.png">
<!-- / is important and dont write public folder-->

Solution 12 - Php

my opinion is that:

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

is the best method to route to your css files.

The URL::to() also works for js

Solution 13 - Php

Laravel version 6.0.2

I also had the same problem. I used

<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet">

And after moving the CSS folder from folder resource to folder public it works

Solution 14 - Php

This worked for me:
If it's a css file tap

<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >

If it's an image tap

<link href="{{ asset('css/img/logo.png') }}" rel="stylesheet" type="text/css" >

Solution 15 - Php

<link rel="stylesheet" href="/css/bootstrap.min.css">if you are using laravel 5 or 6 you should create folder css and call it with

it works for me

Solution 16 - Php

You can simply create assets in public and added more paths and call in your head html the following code

Solution 17 - Php

if you are on ubuntu u can try these commands:

sudo apt install npm

npm install && npm run dev

Solution 18 - Php

Simply you can put a back slash in front of your css link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondavelowe85View Question on Stackoverflow
Solution 1 - PhpKalhan.ToressView Answer on Stackoverflow
Solution 2 - PhpThilina DharmasenaView Answer on Stackoverflow
Solution 3 - PhpDavid JView Answer on Stackoverflow
Solution 4 - PhpPervezView Answer on Stackoverflow
Solution 5 - PhpThuanLeView Answer on Stackoverflow
Solution 6 - PhpUchiha MadaraView Answer on Stackoverflow
Solution 7 - PhpMansoor Ahmad KhanView Answer on Stackoverflow
Solution 8 - PhpMuddasir AbbasView Answer on Stackoverflow
Solution 9 - PhpRomelleView Answer on Stackoverflow
Solution 10 - PhpScrewtape007View Answer on Stackoverflow
Solution 11 - PhpAlamiFatiView Answer on Stackoverflow
Solution 12 - PhpkqcreationsView Answer on Stackoverflow
Solution 13 - PhpMladen NikolicView Answer on Stackoverflow
Solution 14 - PhpselharemView Answer on Stackoverflow
Solution 15 - PhpAlamiFatiView Answer on Stackoverflow
Solution 16 - PhpRafael MouraView Answer on Stackoverflow
Solution 17 - PhpsaifView Answer on Stackoverflow
Solution 18 - PhpSenthooView Answer on Stackoverflow