Home >Backend Development >PHP Tutorial >How Can I Remove the \'/public/index.php\' Prefix from My Laravel URLs?

How Can I Remove the \'/public/index.php\' Prefix from My Laravel URLs?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-09 09:37:07785browse

How Can I Remove the

Removing "public/index.php" from Laravel URLs

Introduction

By default, Laravel generates URLs with the "/public/index.php/" prefix. This additional path component can be unnecessary and distracting, especially in production environments. This article explores two approaches to remove it.

Option 1: Using .htaccess

Create or edit the .htaccess file in the Laravel root directory and add the following code:

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

This rule rewrites all incoming requests to the "public" directory, effectively removing the "/public/index.php/" prefix.

Option 2: Moving Files and Editing Directives

  1. Create a new folder in the root directory and move all files and folders from "public/" into it.
  2. In the "bootstrap/paths.php" file, change the "public" path to point to the new folder outside of "public/".
'public' => __DIR__.'/../../',
  1. In "index.php", update the following lines to match the new structure:
require __DIR__.'/laravel_code/bootstrap/autoload.php';
$app = require_once __DIR__.'/laravel_code/bootstrap/start.php';

This approach physically moves the files out of the "public/" directory and adjusts the path configuration accordingly.

The above is the detailed content of How Can I Remove the \'/public/index.php\' Prefix from My 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