php 避免laravel的public文件夹,直接在web服务器中打开root
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19923091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid public folder of laravel and open directly the root in web server
提问by Sagiruddin Mondal
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.
我只是想避免使用“public”,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 !
&提前感谢互动!
回答by lukaserat
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:
您可以在您的服务器上创建一个文件夹名称 mylaravelsitepublic_html
与public_ftp
文件夹内联,并将除公共文件夹之外的整个 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.
您的网站应该正在运行。
回答by Anik
Create .htaccess in root folder and write these line
在根文件夹中创建 .htaccess 并写入这些行
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
For production server it is recommended that you point your domain directly to the public folder
对于生产服务器,建议您将域直接指向公共文件夹
回答by fideloper
It sounds like the information you are missing is:
听起来您缺少的信息是:
Documentroot
文档根目录
This is also called the "web root". Apache specifically calls it the DocumentRoot- 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.
公平警告:某些托管服务提供商没有为您提供足够的访问权限来实现这一目标。
回答by lukaserat
point your web directory to public/
. If you are using apache this will work:
将您的 Web 目录指向public/
. 如果您使用的是 apache,这将起作用:
<VirtualHost *:80>
DocumentRoot /var/www.mylaravelsite.com/public/
ServerName www.mylaravelsite.com
</VirtualHost>
回答by Muhammad Sadiq
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.
强烈建议不要在没有测试环境的情况下使用上述方法,而应该使用虚拟主机。
回答by Atul Shah
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/ [L]
</IfModule>
回答by Chetan Bhootra
All you just need to change .env file in your laravel project files. if your laravel project inside in
您只需要更改 Laravel 项目文件中的 .env 文件即可。如果你的 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
谢谢
回答by xuanhvh
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 [L,R=301,P]