[php] Laravel 5 not finding css files

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.

This question is related to php .htaccess laravel laravel-5

The answer is


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')) !!}

Here is the solution

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

<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


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


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 :

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


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

To fast apply simple regex.

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

and

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

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.


Stop artisan serve

and try using

php -S localhost:8000 -t public

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


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


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


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


if you are on ubuntu u can try these commands:

sudo apt install npm

npm install && npm run dev

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.


It's works. For css:

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

Try to:

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

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

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()