Remove Public From URL In Laravel Without Htaccess

Hello artisan

In this tutorial i am going to show you the way of how to remove public path from url in laravel. It is must needed thing for good seo to avoid duplicate url. I will do this laravel 8 remove public from url without htaccess file.

I will simply create our own custom code to remove public path from url in laravel application. So from this example you will learn remove public from url in laravel 8 without htaccess.

So let's see the example code of laravel 8 public path remove.


namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
class RouteServiceProvider extends ServiceProvider
    public const HOME = '/home';

    public function boot()
        $this->routes(function () {
     * Write code on Method
     * @return response()
    protected function removePublicPHPFromURL()
        if (Str::contains(request()->getRequestUri(), '/public/')) {
            $url = str_replace('public/', '', request()->getRequestUri());
            if (strlen($url) > 0) {
                header("Location: $url", true, 301);


now if you visit url like bellow then:


Then you will be redirected to the following url:


Read also: Laravel 8 Remove index php From URL Example


Hope it can help you.


#laravel #laravel-8x