如何在Laravel中将公用文件夹设置为根? [英] How to make public folder as root in Laravel?
问题描述
我使用Laravel框架.如您所知,其目录如下:
I use Laravel framework. As you know, its directory looks like this:
要打开我网站的主页( Route :: get('/','HomeController @ index');
),我需要打开以下网站的/public
文件夹目录.换句话说,要查看我网站的首页,请点击以下网址:
To open the homepage of my website (Route::get('/', 'HomeController@index');
) I need to open /public
folder of directory. In other word, to see the first page of my website, here is the URL:
http://example.com/public
无论如何,仅 我的域名( http://example.com/
)不是我的根.如何将/public
文件夹设置为根目录?
anyway, only my domainname (http://example.com/
) isn't my root. How can I make /public
folder as root?
目前,我找到了一种解决方法.我可以在根目录上创建一个 index.php
,并将重定向代码写入/public
文件夹.因此,当用户输入 http://example.com/
时,它将自动重定向到 http://example.com/public
.但这仍然是丑陋的.我不喜欢在URL中看到/public
.有什么建议吗?
Curently I've found a workaround. I can create an index.php
on the root and write a redirect code to /public
folder. So when user enters http://example.com/
, it will be redirected to http://example.com/public
automatically. But still that's ugly. I don't like to see /public
in the URL. Any suggestion?
推荐答案
有很多方法可以做到这一点.
There are many ways to do this.
您只需要从 public 目录中剪切 index.php
和.htaccess并将其粘贴到根目录中,就可以了,并替换 index中的两行.php
为
You just need to cut index.php
and .htaccess from public directory and paste it in the root directory, that's all and replace two lines in index.php
as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
最好的方法是 .htaccess
.在Laravel安装的根目录中创建 .htaccess
文件.下面的代码将适用于此.
Best way to do is .htaccess
. Create .htaccess
file in root directory in Laravel installation. And the below code will work for this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
您可以从此处阅读: 要做的方式
You can read from here: WAY TO DO
这篇关于如何在Laravel中将公用文件夹设置为根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!