Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laraertan's avatar

Is It Possible to set Different Assets Directory

As a default Laravel looking into public folder to load css files. Thats fine. What about if I move public folder to up directory (not subdirectory anymore ) because of share hosting reason. Example :

MyApp (Laravel App)

Public_html (Public Files, css images, js etc)

In this case Laravel still looking for a public folder inside MyApp! Therefore I cant load css files! With L4 I was able to that. Could someone help me with this pls?

As a summary

Here What I done after fresh Install of Laravel 5.1

|_ Main-app

    |_  app

    |_  bootstrap

    |_ ...

|_ Public_html

    |_app
    
        |_index.php

        |_css

            |_master.css

1.Create MyApp.php class inside Laravel>App folder to extend Applicaton.php class

<? php 
namespace App;
class MyApp  extends \Illuminate\Foundation\Application
{
    public function publicPath()

    {

    return $this->basePath.DIRECTORY_SEPARATOR.'/../public_html/app';
    }
}

2.Inside bootsrap/app use own class

comment old one

// $app = new Illuminate\Foundation\Application(

//     realpath(__DIR__.'/../')

// );

$app = new App\MyApp(

realpath(__DIR__.'/../')

);

3.Make changes inside server.php to target index.php

require_once __DIR__.'/../public_html/app/index.php';

4.Make changes inside index.php

require __DIR__.'/../../main-app/bootstrap/autoload.php';

and

$app = require_once __DIR__.'/../../main-app/bootstrap/app.php';

DONE my app working but not loading css or img

0 likes
45 replies
joedawson's avatar

Wait, have you moving everything from /public into your root?

1 like
joedawson's avatar

Just to be clear, you have done the following...

  • Moved everything from /public into your route
  • Renamed your route project to public_html

By doing so, are you aware that your entire app is now public? Let's take your .env file for example, I could just go to http://yourdomain.com/.env and I will have access to your .env file. I would strongly advise you to not do this, for obvious reasons.

I'm not quite sure how people deal with their Laravel apps on shared hosting, but I'm sure there are a few of threads on here to help you avoiding this huge door you're opening with regards to your apps security.

At least this how I'm assuming your current set up is.

Laraertan's avatar

Just to make clear myself I am keeping separate my Laravel app and my public folder. My Laravel App on root and my public files inside public_html folder (other name www).

Sorry for the Confusion

joedawson's avatar

Oh, ok! Sorry - when I asked

Wait, have you moving everything from /public into your root?

And you said yes, I thought you had moved everything up from public into your main app. Not to worry! As @askaoru suggested, check out that link :)

askaoru's avatar

@Laraertan , are you generating the css using the asset() function? and try checking on the source, where are they trying to find your css at?

Laraertan's avatar

I am keeping my css file like;

Public_html>App>Css>Master.css

And Calling from welcome.blade.php

< link href="css/master.css" type="text/css" rel=" stylesheet " >

Nothing complicated but doesnt work. I had notice If I create public folder inside Laravel app (same as default Laravel installation ) and creating empty master.css file then working!

MyApp>Public>Css>Master.css ---> empty css file

Public_html>App>Css>Master.css ---> my style here

Like this working! What is wrong?

joedawson's avatar

Can you provide a screenshot of your app's structure please? I'm struggling to make sense of where your files are.

To me, it sounds like you have your CSS inside of an /app/css folder - which is wrong.

1 like
askaoru's avatar

try instead use.

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

see if that works

, and yes. a screenshot of your app structure would be better for us to find the problem

Laraertan's avatar

Here What I done after fresh Install of Laravel 5.1

|_ Main-app

    |_  app

    |_  bootstrap

    |_ ...

|_ Public_html

    |_App
    
        |_index.php

        |_css

            |_master.css

1.Create MyApp.php class inside Laravel>App folder to extend Applicaton.php class

<? php 
namespace App;
class MyApp  extends \Illuminate\Foundation\Application
{
    public function publicPath()

    {

    return $this->basePath.DIRECTORY_SEPARATOR.'/../public_html/app';
    }
}

2.Inside bootsrap/app use own class

comment old one

// $app = new Illuminate\Foundation\Application(

//     realpath(__DIR__.'/../')

// );

$app = new App\MyApp(

realpath(__DIR__.'/../')

);

3.Make changes inside server.php to target index.php

require_once __DIR__.'/../public_html/app/index.php';

4.Make changes inside index.php

require __DIR__.'/../../main-app/bootstrap/autoload.php';

and

$app = require_once __DIR__.'/../../main-app/bootstrap/app.php';

DONE my app working but not loading css or img

askaoru's avatar

well @Laraertan . Well I guess the problem is. Since you are calling the css not using the asset() function, you need to provide the full css path from the public_html directory.

This should work.

<link href="/app/css/master.css" type="text/css" rel="stylesheet">
<img src="/app/img/logo.png">
Laraertan's avatar

Chrome gives me GET error (not finding)

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

But

<link href="/css/master.css" type="text/css" rel="stylesheet">

with this I got no error but not able to load css

Chrome says:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/css/master.css".

Laraertan's avatar

Indeed! Still no luck , I couldn't make this work! Thanks for your effort

Snapey's avatar

Seems a lot of work, not sure what you are gaining

if your root folder is public_html then your css is at /app/css note the capitalisation

but I'm confused, which is the root folder?

Laraertan's avatar

@snapey

Main-app is "Laravel Project" and "public_html" is other word www directory

Pls don't get confuse because I called example folder name as app inside public_html.That could be any name.

so here is my structure

|_ Main-app

    |_  app

    |_  bootstrap

    |_ ...

|_ Public_html

    |_app
    
        |_index.php

        |_css

            |_master.css
Snapey's avatar

So you are saying to get to your index page it is domain.com/App/index.php ?

Or have you messed with the .htaccess file also to hide this?

Laraertan's avatar

Thats right! I am running more than one project so I call this Laravel project domain.com/App/index.php

Laraertan's avatar

L4 working great same as this structure but L5 having problem to load css!

Snapey's avatar

So, of course, your css should be loaded with /app/css/my.css

Just make sure of the letter case. In this thread you have referred to App and app

Laraertan's avatar

Unfortunately doesn't work like that Chrome gives me GET error.

And if i use /css/my.css no error but

Chrome says :

Resource interpreted as Stylesheet but transferred with MIME type text/html:

Snapey's avatar

Looks like your .htaccess is interfering in some way with the delivery of the css

Laraertan's avatar

this is my .htaccess

public_html>app> .htaccess

<IfModule mod_rewrite.c>

Options +FollowSymLinks
RewriteEngine On

AddType text/css .css

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

</IfModule>

I am almost got crazy for this error! Thanks for your time

Laraertan's avatar

Most awkward things is Why this way is working?

Main-app>public>css>my.css even empty but now I can load css from

Public_html>app>css>my.css

|_ Main-app
    |_  app
    |_  bootstrap
    |_  public
            |_ css
                |_ my.css   ( This file empty )
    |_ ...

|_ Public_html
    |_app
        |_index.php
        |_css
            |_ my.css   ( This file has a style  and welcome.blade.php can load css)
Snapey's avatar

send me the domain name? you can use the contact form on the website you mentioned earlier.

Laraertan's avatar

Well @jlrdw the link you mentioned also have discussion about my situation.But not clear solution provided except symlink! I am not keeping publicly my project files like .env.

Server: Project and Root directory on the same level

/home/somesharedaccount23424/my_laravel_site/


/home/somesharedaccount23424/public_html     (Web Root (public_html/www))

L4 does perfect job but L5 really painful to sort things like that

@Snapey thanks for your effort, I am trying to do locally so when I deploy simply I can use ssh and git to keep my web app update.

Currently L5 project not on the server but L4 on the server and running with this structure

Next

Please or to participate in this conversation.