How to remove public from url in laravel 9

Asked by: frankl79
Date:
Viewed: 150
Answers: 2
  • 1

Is it possible to remove “public” from the URL for a Laravel application and just safely upload the entire app folder to my public_html folder on my server? Thanks

Answers

Answer by: ChristianKovats

Answered on: 25 Jan 2023

  • 0

In your root Laravel app, create a file called server.php with the following content:

<?php

$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);


if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}

require_once __DIR__.'/public/index.php';

Then create a .htaccess file in the same place with the following content

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (.w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>

<Files .env>
order allow,deny
Deny from all
</Files>

 

Now you should be able to access your site without the “public” in the url. But as Boon said, it’s not the intended way to use Laravel.

You should also check if you can access your .env file like so: yoursite.com/.env

You should get a 404 or access forbidden error.

 

Answer by: boon

Answered on: 25 Jan 2023

  • 0

You could just run

php artisan serve

and you won’t have the “public” in the url. It’s not safe to mess around with htaccess.

Please log in to post an answer!