Default index.php Root Path Change And Remove Public From The Url

  • 24-06-2022
  • 1037
  • Laravel 9
  • Haresh Chauhan

Laravel Default root path gives us inside the public directory where your index file is given and then run the whole project from there index.php file.

This index file is always first run before the run of other files and assets. So this index file most important file in the project. And from this index file, it will run the vendor folder all the classes and basic framework of the project.

Also there with a .htaccess file in the "public" folder. This is also important file for the URL acceptance.

But sometimes developers want that they don't want to keep the index.php file inside the public folder they want to put it on the base root so they can easily access the index file from the base root path of the laravel applications.

So in this post, I will learn you how change the default root path of the laravel application?.

I want to put my index.php file outside the public folder. That mean run this index file from the root path don't want to step inside the public folder.

public/index.php To index.php (Outside The Public Folder)

This is my index.php file where I made some modifications. The default index.php file always having inside the public folder but I want to change this path to the base root path.

So just change accordingly below index.php in your laravel application index.php file. just we need to change the path from the public the root.

I just made some changes in my path nothing else. Stepping out from the public. Now our index file will run from the base root path of the application. But the job is not done yet we need more steps.

public/index.php To index.php (Outside The Public Folder)

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/


{{-- if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
} --}}


 {{-- REPLACE TO --}}

if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
    require $maintenance;
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

{{-- require __DIR__.'/../vendor/autoload.php'; --}}

{{-- REPLACE TO  --}}

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

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

{{-- $app = require_once __DIR__.'/../bootstrap/app.php'; --}}

{{-- REPLACE TO  --}}

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

$kernel = $app->make(Kernel::class);

$response = $kernel->handle(
    $request = Request::capture()
)->send();

$kernel->terminate($request, $response);

Once you change your index.php file path just copy this file from the public folder and paste outside the public folder.

public/.htaccess To .htaccess (Outside The Public Folder)

After paste the index.php file from the public folder to the outside root project. We also need to paste .htaccess file from the public folder.

Put the were you paste the index.php file. We must need both files index.php and .htaccess same root else your project is not able to run.

public/.htaccess To .htaccess (Outside The Public Folder)

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

After path the .htaccess file from the public folder, Next we need to change server.php. It's already on the root path of the project.file name with server.php.

server.php

In this file we just need to Remove "public" from the require_once. That means now your serve command will run from the same root where this file having.

You can see in this example I removed "public" before the index.php file.

<?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'; --}}

{{-- REPLACE TO --}}

require_once __DIR__.'/index.php';

webpack.mix.js

Please also check this file if there is no apply "public" in the mixin then please apply mixin asset with the "public" so they can go to the public folder and then apply.

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css');

Assets Path | asset()

Here if you are usign asset() function for run your asset from the public folder like links and scripts you need to add public for all the running assets.

{{-- LINKS --}}

<link rel="stylesheet" href="{{ asset('css/app.css') }}">

{{-- REPLACE TO --}} {{-- REPLACE TO --}}

<link rel="stylesheet" href="{{ asset('public/css/app.css') }}">

[issue] Possible All Way To Change Asset Path And Add Public To Asset Path In Laravel

{{-- SCRIPTS --}}

<script src="{{ asset('js/app.js') }}"></script>

{{-- REPLACE TO --}} {{-- REPLACE TO --}}

<script src="{{ asset('public/js/app.js') }}"></script>

Laravel Remove index.php From URL


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :