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
Release: 2024-11-25 15:08:13
Original
877 people have browsed it

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>
Copy after login

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',
Copy after login

Replace it with:

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

Replace it with:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template