如何在URL生成的Laravel中删除"public/index.php"? [英] How Can I Remove “public/index.php” in the URL Generated Laravel?

查看:290
本文介绍了如何在URL生成的Laravel中删除"public/index.php"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Laravel中生成的URL中删除index.phppublic/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中找到以下代码行:

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

并将其更改为:

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

index.php中,找到以下行:

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';

来源:如何从以下网址中的URL删除/public/Laravel

这篇关于如何在URL生成的Laravel中删除"public/index.php"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆