Laravel 5 – Remove Public from URL

Laravel redirect root path to public directory

<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>

 

Another Approach

Step 1. Move two files from public directory index.php and .htaccess to Root directory.

Step 2. open index.php file replace with

 require __DIR__.'/../bootstrap/autoload.php';

to

 require __DIR__.'/bootstrap/autoload.php';

and

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

to

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

And remove all cache and cookies.

Step 3. Change public path after that.

$app->bind('path.public', function() {
return realpath(__DIR__.'/public');
});

Step 4. Change server.php

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

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

To 

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

require_once __DIR__.'/index.php';

 

Reference : https://stackoverflow.com/questions/28364496/laravel-5-remove-public-from-url

 

Leave a Reply

Your email address will not be published.