How Can I Remove “public/index.php” in the URL Generated Laravel? How Can I Remove “public/index.php” in the URL Generated Laravel? laravel laravel

How Can I Remove “public/index.php” in the URL Generated Laravel?


Option 1: Use .htaccess

If it isn't already there, create an .htaccess file in the Laravel root directory.Create a .htaccess file your Laravel root directory if it does not exists already. (Normally it is under your public_html folder)

Edit the .htaccess file so that it contains the following code:

<IfModule mod_rewrite.c>   RewriteEngine On    RewriteRule ^(.*)$ public/$1 [L]</IfModule>

Now you should be able to access the website without the "/public/index.php/" part.

Option 2 : Move things in the '/public' directory to the root directory

Make a new folder in your root directory and move all the files and folder except public folder. You can call it anything you want. I'll use "laravel_code".

Next, move everything out of the public directory and into the root folder. It should result in something somewhat similar to this:enter image description here

After that, all we have to do is edit the locations in the laravel_code/bootstrap/paths.php file and the index.php file.

In laravel_code/bootstrap/paths.php find the following line of code:

'app' => __DIR__.'/../app','public' => __DIR__.'/../public',

And change them to:

'app' => __DIR__.'/../app','public' => __DIR__.'/../../',

In index.php, find these lines:

require __DIR__.'/../bootstrap/autoload.php';     $app = require_once __DIR__.'/../bootstrap/start.php';

And change them to:

require __DIR__.'/laravel_code/bootstrap/autoload.php';     $app = require_once __DIR__.'/laravel_code/bootstrap/start.php';

Source: How to remove /public/ from URL in Laravel


I tried this on Laravel 4.2

Rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.

I hope it works


Don't get confused with other option the snippet below I am using and will be useful for you too.Put the below htacces in your root.

<IfModule mod_rewrite.c>    RewriteEngine on    RewriteRule    ^$    public/    [L]    RewriteRule    (.*) public/$1    [L]</IfModule>

Go to your public directory and put another htaccess file with the code snippet below

<IfModule mod_rewrite.c>    RewriteEngine On    # Removes index.php from ExpressionEngine URLs      RewriteCond %{REQUEST_FILENAME} !-f    RewriteCond %{REQUEST_FILENAME} !-d    RewriteRule ^(.*)$ index.php?url=$1 [PT,L]</IfModule>

Its working... !!!

Laravel Uses below .htaccess

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