How to Remove “public” from URLs
Laravel developers often face an issue where the public folder appears in their URLs. By default, Laravel serves the application through the public directory, but for clean and professional URLs, it’s essential to remove it.
In this guide, we’ll explore how to properly remove the public folder from Laravel URLs.
Method 1: Using .htaccess File
A simple way to resolve this issue is by modifying the .htaccess file in the root directory of your Laravel project. Follow these steps:
- Create a new
.htaccessfile in the root directory of your Laravel project (if it doesn’t exist already). - Add the following code to the file:
<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>
Create server.php code in root directory of Laravel project
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Laravel Page Not Found Error 404 Not Found, but route exist in Laravel
Column not found: 1054 Unknown column ‘0’ in ‘field list’ – Laravel – I don’t have a 0 column
