How to remove /public/ from a Laravel URL?
To remove the public path to the main domain in Laravel without changing the CSS and image file references, you can follow these steps:
1. create a .htaccess file in the root directory (if it doesn't exist already) and add the following code to handle the redirection:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
This code will redirect all requests to the public folder while keeping the URL intact.
2. Open the index.php file located in the public folder and add the following lines on top:
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
if(stristr($uri, '/public/') == TRUE) {
if(file_exists(__DIR__.'/public'.$uri)){
}else{
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = str_replace('public/', '',$actual_link);
header("HTTP/1.0 404 Not Found");
header("Location: ".$actual_link."");
exit();
return false;
}}
This peace of code will remove public from the url and will give a 404 and then redirects to the url without the public.
After completing these steps, when you access your Laravel application through the main domain, the requests will be redirected to the public folder without any changes to the CSS and image file references.