如何删除 Laravel 生成的 URL 中的“public/index.php"? [英] How Can I Remove “public/index.php” in the URL Generated Laravel?
问题描述
我需要从 Laravel 中生成的 URL 中删除 index.php
或 public/index.php
;一般路径是localhost/public/index.php/someWordForRoute
,应该是localhost/someWordForRoute.
I need to remove index.php
or public/index.php
from the generated URL in Laravel; commonly path is localhost/public/index.php/someWordForRoute
, It should be something like localhost/someWordForRoute.
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes.
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php[L]
app/config/app.php
'url' => 'http://localhost',
我该如何改变?
推荐答案
选项 1:使用 .htaccess
如果它不存在,请在 Laravel 根目录中创建一个 .htaccess 文件.如果 Laravel 根目录不存在,则创建一个 .htaccess
文件.(通常它在您的 public_html
文件夹下)
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)
编辑 .htaccess 文件,使其包含以下代码:
Edit the .htaccess file so that it contains the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
现在您应该可以在没有/public/index.php/"部分的情况下访问该网站.
Now you should be able to access the website without the "/public/index.php/" part.
在您的根目录中创建一个新文件夹并移动除公共文件夹之外的所有文件和文件夹.你可以随意称呼它.我将使用laravel_code".
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:
之后,我们要做的就是编辑 laravel_code/bootstrap/paths.php
文件和 index.php
文件中的位置.
After that, all we have to do is edit the locations in the laravel_code/bootstrap/paths.php
file and the index.php
file.
在 laravel_code/bootstrap/paths.php
中找到以下代码行:
In laravel_code/bootstrap/paths.php
find the following line of code:
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../public',
并将它们更改为:
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../../',
在 index.php
中,找到以下几行:
In index.php
, find these lines:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
并将它们更改为:
require __DIR__.'/laravel_code/bootstrap/autoload.php';
$app = require_once __DIR__.'/laravel_code/bootstrap/start.php';
这篇关于如何删除 Laravel 生成的 URL 中的“public/index.php"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!