davincho's avatar

Removing Public from Laravel URL

Hello, i have a problem, how config my .htaccess or my config with my project in need delete the public on the URL for example my url is

name.domain.com/example/public/

i need change whit this

name.domain.com/example/

i see examples and tutorials about this config but not function in laravel 8

help me please!

Thanks.

0 likes
8 replies
laracoft's avatar

@davincho

Point your DocumentRoot to the correct folder. You are likely pointing to 1, that's why you have a public in your URL. You need to point it to #2 instead

    └── app                 <- 1. Laravel root folder, DocumentRoot CANNOT point here
        ├── public          <- 2. DocumentRoot MUST point here, i.e. https://yourdomain.com
        │   └── robots.txt  <- 3. MUST be able to load https://yourdomain.com/robots.txt
        ├── storage
        ...
        └── vendor
1 like
jlrdw's avatar

I would suggest setting up the correct secure folder structure.

Main laravel shouldn't even be under (htdocs, or public_html, or www).

Main laravel should be a folder higher.

FIles in public go in the web folder only.

Just example.

Main laravel here (laravel54up) is outside of, not in, and above public_html.

But just a suggestion....

1 like
davincho's avatar

hi thanks for your answers, the problem is that my host is a shared host and I don't have permissions for the htdocs folder, and the domain hosts different pojects for example

subdomain.domain.com/project1 subdomain.domain.com/project2 subdomain.domain.com/project3

and i don't want to show public folder in url.

laracoft's avatar
laracoft
Best Answer
Level 27

@davincho

In that case, you must do this

├── public_html         <- 1. DocumentRoot MUST point here
│   └── blog            <- 3. move and rename `public` as `blog`, https://yourdomain.com/blog
│       ├── robots.txt  <- 4. MUST be able to load https://yourdomain.com/blog/robots.txt
│       └── index.php   <- 5. Must modify this index.php
└── blogapp             <- 2. Laravel root folder, DocumentRoot CANNOT point here
    ├── storage
    ...
    └── vendor
// find an change these 2 lines in public_html/blog/index.php 
...
// require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../../blogapp/bootstrap/autoload.php';
...
// $app = require_once __DIR__.'/../bootstrap/app.php';
$app = require_once __DIR__.'/../../blogapp/bootstrap/app.php';
...
1 like
davincho's avatar

Thanks, the problem is done, your solution fix my problem.

Snapey's avatar

editing index.php is a LAST RESORT and you must make absolutely sure that your .env file is not exposed.

1 like
jlrdw's avatar

Yet it's so clear-cut if you follow it step-by-step. Another problem is they go for cheap shared hosting sometimes where you cannot move main laravel out of Webroot on some hosts.

Also the same technique has been done on a YouTube video, where a simple search would reveal it.

Surely anyone should know by now, and as long as laravel has been around there has got to be YouTube videos on laravel.

But making sure .env is not exposed is definitely better than nothing.

Please or to participate in this conversation.