简体   繁体   English

避开 laravel 的公用文件夹,直接在 web 服务器中打开根目录

[英]Avoid public folder of laravel and open directly the root in web server

I was going through all possible sample on internet to solve this.我正在浏览互联网上所有可能的样本来解决这个问题。 Still it is an headache.还是很头疼。

I just want to avoid the 'public' in www.mylaravelsite.com/public/ and make it like www.mylaravelsite.com for the root directory.我只是想避免www.mylaravelsite.com/public/中的“公共”,并将其设置为www.mylaravelsite.com作为根目录。

Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.现在我不想避免安全问题,所以我了解到.htaccess将是最好的方法。

Any solution friends?有解决的朋友吗?

& advance thanks for interacting !并提前感谢您的互动!

Let's assume you have this folder structure in your server假设您的服务器中有此文件夹结构

.cpanel/
public_html/
public_ftp/
..

And the laravel folder structure is而laravel文件夹结构是

app/
bootstrap/
public/
vendor/
composer.json
artisan
..

You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html , so you have now:您可以在您的服务器上创建一个文件夹名称 mylaravelsite 与public_htmlpublic_ftp文件夹内联,并将除 public 文件夹之外的整个 Laravel 应用程序复制到其中,因为您会将所有内容粘贴到public_html ,因此您现在拥有:

.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...

On your public_html/index.php change the following line:在您的public_html/index.php更改以下行:

require __DIR__.'/../bootstrap/autoload.php';

$app = require_once __DIR__.'/../bootstrap/start.php';

to

require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';

$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';

and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.并且不要忘记更改/mylaravelsite/bootstrap/paths.php公共路径,您可能会使用它。

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

to

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

Your site should be running.您的网站应该正在运行。

Create .htaccess in root folder and write these line在根文件夹中创建 .htaccess 并写入这些行

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

For production server it is recommended that you point your domain directly to the public folder对于生产服务器,建议您将域直接指向公共文件夹

It sounds like the information you are missing is:听起来您缺少的信息是:

Documentroot文档根目录

This is also called the "web root".这也称为“网络根”。 Apache specifically calls it theDocumentRoot - Click that link for the official explanation. Apache 专门将其称为DocumentRoot - 单击该链接以获取官方解释。

Basically what it does is set which directory the server pulls files from.基本上它所做的是设置服务器从哪个目录中提取文件。 In Laravel, the document root should be the public folder.在 Laravel 中,文档根目录应该是public文件夹。

Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.将 DocumentRoot 设置为public意味着在浏览器中访问http://mylaravelsite.com实际上将根据需要“指向” public文件夹。

In the .htaccess file (actually, more likely in the virtual host configuration ), you can set the DocumentRoot for your site:在 .htaccess 文件中(实际上,更可能在虚拟主机配置中),您可以为您的站点设置DocumentRoot

DocumentRoot /path/to/laravel-app/public

How you set this up depends on your hosting.您如何设置取决于您的主机。 Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support.每个主机都有不同的网站设置方式,因此我们无法专门为您解答——您应该咨询您的网络主机技术支持。 The key point is that the public directory should be the web root, while everything else should be "behind the web root".关键是public目录应该是网络根目录,而其他一切都应该“在网络根目录之后”。

Fair warning tho: some hosting providers do not give you enough access to accomplish that.公平警告:某些托管服务提供商没有为您提供足够的访问权限来实现这一目标。

point your web directory to public/ .将您的网络目录指向public/ If you are using apache this will work:如果您使用的是 apache,这将起作用:

<VirtualHost *:80>
  DocumentRoot /var/www.mylaravelsite.com/public/
  ServerName www.mylaravelsite.com
</VirtualHost>

Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,如果它不存在,只需在 Laravel 根目录上添加 .htaccess 文件并在其中添加以下几行,就是这样,

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

Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as这是解决方案,但不能在实际项目中这样做,它只是为了测试 Laravel 的初学者,我已经用 Laravel 5.0 对其进行了测试,它是从公共文件夹中剪切 index.php 和 .htaccess 文件并将其粘贴到根目录并将这两行更改为

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

It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.强烈建议不要在没有测试环境的情况下使用上述方法,而应该使用虚拟主机。

create .htacess file in the root directory of your laravel project and put this code in.在 Laravel 项目的根目录中创建 .htacess 文件并将此代码放入。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

All you just need to change .env file in your laravel project files.您只需要更改 Laravel 项目文件中的 .env 文件即可。 if your laravel project inside in如果你的 Laravel 项目在

Root/domain/public_html/laravel/根/域/public_html/laravel/

Go to .env file and edit转到 .env 文件并编辑

you need to edit app url path which should be您需要编辑应为的应用程序网址路径

APP_URL= http://www.domainname.com/laravel/public/ APP_URL= http://www.domainname.com/laravel/public/

Thats it your project will run now.就是这样,您的项目现在将运行。 if your laravel files is in public_html, then your app url must be like this如果你的 Laravel 文件在 public_html 中,那么你的应用 url 必须是这样的

APP_URL= http://www.domainname.com APP_URL= http://www.domainname.com

Thanks谢谢

For Lumen对于流明

Let's think you have a folder path like: /var/www/project/microservice(lumen src)假设您有一个文件夹路径,如:/var/www/project/microservice(lumen src)

/var/www => document root for localhost/ /var/www => 本地主机的文档根目录/

Target you want: localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...你想要的目标: localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...

I do this by .htaccess (placed at /project folder) like below:我通过 .htaccess (放置在 /project 文件夹中)来做到这一点,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]

On Server(Apache) Create .htaccess in the root folder and write below line在服务器(Apache)在根文件夹中创建.htaccess并写下一行

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Lean more on Gist更多地了解Gist

On Local Development You can create a virtual host by adding the below line to your apache vhost config file.在本地开发中,您可以通过将以下行添加到 apache 虚拟主机配置文件中来创建虚拟主机。 Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file一般可以在C:\xampp\apache\conf\extra\httpd-vhosts.conf文件中找到这个文件

<VirtualHost *:80>
  DocumentRoot "C:/xampp/htdocs/mysite/public/"
  ServerName www.mysite.local
</VirtualHost>

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

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