简体   繁体   English

如何在 Laravel 中以 root 身份创建公共文件夹?

[英]How to make public folder as root in Laravel?

I use Laravel framework.我使用 Laravel 框架。 As you know, its directory looks like this:如您所知,它的目录如下所示:

在此处输入图片说明

To open the homepage of my website ( Route::get('/', 'HomeController@index'); ) I need to open /public folder of directory.要打开我网站的主页( Route::get('/', 'HomeController@index'); ),我需要打开目录的/public文件夹。 In other word, to see the first page of my website, here is the URL:换句话说,要查看我网站的第一页,这里是 URL:

http://example.com/public

anyway, only my domainname ( http://example.com/ ) isn't my root.无论如何,只有我的域名( http://example.com/ )不是我的根。 How can I make /public folder as root?如何以 root 身份创建/public文件夹?


Curently I've found a workaround.目前我找到了一个解决方法。 I can create an index.php on the root and write a redirect code to /public folder.我可以在根目录上创建一个index.php并将重定向代码写入/public文件夹。 So when user enters http://example.com/ , it will be redirected to http://example.com/public automatically.因此,当用户输入http://example.com/ ,它将自动重定向到http://example.com/public But still that's ugly.但这仍然是丑陋的。 I don't like to see /public in the URL.我不喜欢在 URL 中看到/public Any suggestion?有什么建议吗?

There are many ways to do this.有很多方法可以做到这一点。

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您只需要从公共目录中剪切index.php和 .htaccess 并将其粘贴到根目录中即可,将index.php中的两行替换为

require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';

Best way to do is .htaccess .最好的方法是.htaccess Create .htaccess file in root directory in Laravel installation.在 Laravel 安装的根目录中创建.htaccess文件。 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您可以从这里阅读: WAY TO DO

Do not mofidy any Laravel files.不要修改任何 Laravel 文件。 Use web server (Apache or Nginx) to point Laravel project to public directory .使用 Web 服务器(Apache 或 Nginx)将Laravel 项目指向public目录

For Apache you can use these directives:对于 Apache,您可以使用这些指令:

DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">

For nginx, you should change this line:对于 nginx,您应该更改此行:

root /path_to_laravel_project/public;

Step 1: Put your /public/index.php and /public/htaccess file to your root directory as /index.php and /htaccess .第 1 步:将您的/public/index.php/public/htaccess文件作为/index.php/htaccess放入您的根目录。
Step 2: Now Make changes in your index.php第 2 步:现在在 index.php 中进行更改

require __DIR__.'/../bootstrap/autoload.php'; //OLD code
$app = require_once __DIR__.'/../bootstrap/app.php';

to

 require __DIR__.'./bootstrap/autoload.php'; //NEW code
 $app = require_once __DIR__.'./bootstrap/app.php';

For Ubuntu 20.04 do the following for virtual hosts.对于Ubuntu 20.04 ,对虚拟主机执行以下操作。 Also after doing wait for few minutes, clear the browser cache and reload.同样在等待几分钟后,清除浏览器缓存并重新加载。

sudo mkdir /var/www/<your domain>/public
sudo chown -R $USER:$USER /var/www/<your domain>
sudo nano /etc/apache2/sites-available/<your domain>.conf

Then in the file paste然后在文件中粘贴

<VirtualHost *:80>
    ServerName <your domain>
    ServerAlias www.<your domain>
    ServerAdmin <your email address>
    DocumentRoot /var/www/<your domain>/public
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then run然后运行

sudo a2ensite <your domain>
sudo a2dissite 000-default
sudo systemctl reload apache2

nano /var/www/<your domain>/public/index.html

Then paste in file然后粘贴到文件中

<html>
  <head>
    <title><your domain></title>
  </head>
  <body>
    <center>
        <p>Site Under Maintenance</p>
    </center>
  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM