Home >Backend Development >PHP Tutorial >How to Remove \'public/index.php\' from Laravel URLs?

How to Remove \'public/index.php\' from Laravel URLs?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-25 15:08:13896browse

How to Remove

Eliminating "public/index.php" from Laravel URLs

Question: I need to remove "public/index.php" or "index.php" from the generated URL in Laravel. Currently, it's in the format "localhost/public/index.php/someWordForRoute," but I want it to be "localhost/someWordForRoute." How can I achieve this?

Option 1: Using .htaccess

  1. Create an .htaccess file in the Laravel root directory, if it doesn't exist.
  2. Edit the .htaccess file to include the following code:
<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/ [L]
</IfModule>

Option 2: Moving Assets to the Root Directory

  1. Create a new folder in the root directory and name it "laravel_code" (or any name of your choice).
  2. Move all files and folders from the "public" directory to the root folder.
  3. Edit the following code in "laravel_code/bootstrap/paths.php":
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../public',

Replace it with:

'app' => __DIR__.'/../app',
'public' => __DIR__.'/../../',
  1. Edit the following lines in "index.php":
require __DIR__.'/../bootstrap/autoload.php';     
$app = require_once __DIR__.'/../bootstrap/start.php';

Replace it with:

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

The above is the detailed content of How to Remove \'public/index.php\' from Laravel URLs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn