php Laravel 5 – 从 URL 中删除公共
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28364496/
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
Laravel 5 – Remove Public from URL
提问by user1537360
I know this is a very popular question but I haven't been able to find a working solution for Laravel 5. I've been trying to migrate from Codeigniter for a long time, but this convoluted installation process keeps putting me off.
我知道这是一个非常受欢迎的问题,但我一直无法找到适用于 Laravel 5 的解决方案。我一直在尝试从 Codeigniter 迁移很长时间,但这个复杂的安装过程一直让我望而却步。
I don't want to run a VM, this just seems awkward when switching between projects.
我不想运行虚拟机,这在项目之间切换时似乎很尴尬。
I don't want to set my document root to the public folder, this is also awkward when switching between projects.
我不想将我的文档根目录设置为公共文件夹,这在项目之间切换时也很尴尬。
I've tried the .htaccess mod_rewrite method
我试过 .htaccess mod_rewrite 方法
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
This just gives me a Laravel NotFoundHttpException in compiled.php line 7610.
这只是在compiled.php 行7610 中给了我一个Laravel NotFoundHttpException。
When I tried L4 a while ago, I used the method of moving the contents of the public folder into the root. The structure of L5 is quite different and following the same steps completely broke Laravel (the server would only return a blank page).
前阵子试L4的时候,用的是把public文件夹的内容移到root的方法。L5 的结构完全不同,遵循相同的步骤完全破坏了 Laravel(服务器只会返回一个空白页面)。
Is there a decent method of removing 'public' in a development environment that:
在开发环境中是否有一种体面的方法来删除“公共”:
- Works with L5
- Allows me to switch between projects with ease (I'm usually working on 2 or 3 at any one time).
- 适用于 L5
- 允许我轻松地在项目之间切换(我通常一次处理 2 或 3 个)。
Thanks
谢谢
** I'm using MAMP and PHP 5.6.2
** 我使用的是 MAMP 和 PHP 5.6.2
回答by Asfaq Tamim
For Laravel 5:
对于 Laravel 5:
- Rename
server.php
in your Laravel root folder toindex.php
- Copy the
.htaccess
file from/public
directory to your Laravel root folder.
server.php
在 Laravel 根文件夹中重命名为index.php
- 将
.htaccess
文件从/public
目录复制到 Laravel 根文件夹。
That's it!
就是这样!
回答by ka_lin
I have solved the issue using 2 answers:
我已经使用 2 个答案解决了这个问题:
- Renaming the server.php to index.php (no modifications)
- Copy the .htaccess from public folder to root folder (like rimon.ekjon said below)
Changing .htaccess it a bit as follows for statics:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ / [L,R=301] RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public// [L,NC]
If there are any other static filesneeded just add the extension to the previous declared list
- 将 server.php 重命名为 index.php(无修改)
- 将 .htaccess 从公共文件夹复制到根文件夹(如下面所说的 rimon.ekjon)
对于静态,将 .htaccess 稍微更改如下:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ / [L,R=301] RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(css|js|images)/(.*)$ public// [L,NC]
如果需要任何其他静态文件,只需将扩展名添加到先前声明的列表中
回答by Raham
In Laravel 5.5 create .htacess file in your root directory and placed the following code:- Reference Link
在 Laravel 5.5 中,在根目录中创建 .htacess 文件并放置以下代码:-参考链接
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^ [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
回答by Derk Jan Speelman
DON'T!
别!
YOU REALLY SHOULD NOTrename server.php
in your Laravel root folder to index.php
and copy the .htaccess
file from the /public
directory to your Laravel root folder!!!
你真的不应该server.php
在你的 Laravel 根文件夹中重命名index.php
并将.htaccess
文件从/public
目录复制到你的 Laravel 根文件夹!!!
This way everyone can access some of your files (.env
for example). Try it yourself. You don't want that!
这样每个人都可以访问您的某些文件(.env
例如)。自己试试吧。你不想那样!
DO
做
Instead, you should create an .htaccess
file in your root like this:
相反,您应该.htaccess
在根目录中创建一个文件,如下所示:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/ [L,QSA]
This will silently rewrite all your base URIs to the /public
folder. Even all Headers, for example the HTTP Authorization Header, and all optional URI parameters will silently be passed on to the /public
folder as well.
这将默默地将所有基本 URI 重写到/public
文件夹中。甚至所有Headers,例如HTTP Authorization Header,以及所有可选的 URI 参数也将被静默传递到/public
文件夹。
That's all
就这样
回答by Muhammad Sadiq
Easy way to remove public from laravel 5 url. You just need to cut index.php and .htaccess from public directory and paste it in the root directory,thats all and replace two lines in index.php as
从 Laravel 5 url 中删除 public 的简单方法。您只需要从公共目录中剪切 index.php 和 .htaccess 并将其粘贴到根目录中即可,将 index.php 中的两行替换为
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Note: The above method is just for the beginners as they might be facing problem in setting up virtual host and The best solution is to setup virtual host on local machine and point it to the public directory of the project.
注意:以上方法仅适用于初学者,因为他们可能在设置虚拟主机时遇到问题,最好的解决方案是在本地机器上设置虚拟主机并将其指向项目的公共目录。
回答by Miron
@rimon.ekjon said:
@rimon.ekjon 说:
Rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder. -- Thats it !! :)
将 Laravel 根文件夹中的 server.php 重命名为 index.php 并将 .htaccess 文件从 /public 目录复制到 Laravel 根文件夹。 - 就是这样 !!:)
That's working for me. But all resource files in /public directory couldn't find and request urls didn't work, because I used asset() helper.
这对我有用。但是 /public 目录中的所有资源文件都找不到并且请求 url 不起作用,因为我使用了 asset() 助手。
I changed /Illuminate/Foundation/helpers.php/asset() function as follows:
我改变了 /Illuminate/Foundation/helpers.php/asset() 函数如下:
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
Now everything works :)
现在一切正常:)
Thank you @rimon.ekjon and all of you.
谢谢@rimon.ekjon 和你们所有人。
回答by Abhinav Saraswat
Just create .htaccessfile at root and add these lines to it
只需在根目录下创建.htaccess文件并将这些行添加到其中
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
That's it!
就是这样!
The above code works on the root public_html folder. Having said that your core laravel file should be inside the public_html folder, if your directory looks like public_html/laravelapp/public and if you put the above code inside laravelapp it won't work. Therefore you must copy all your core files into public_html and put the .htaccess file there .
上面的代码适用于根 public_html 文件夹。已经说过你的核心 laravel 文件应该在 public_html 文件夹中,如果你的目录看起来像 public_html/laravelapp/public 并且如果你把上面的代码放在 laravelapp 中,它就不会工作。因此,您必须将所有核心文件复制到 public_html 并将 .htaccess 文件放在那里。
If you want to keep the code in a subdirectory then you can create a subdomain then this code will work for that also.
如果您想将代码保存在子目录中,那么您可以创建一个子域,那么此代码也适用于此。
回答by Pallav Nagar
Create .htaccess
file in rootdirectory and place code something like below.
.htaccess
在根目录中创建文件并放置如下代码。
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^ [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
回答by Jeremy Schaffer
1) I haven't found a working method for moving the public directory in L5. While you can modify some things in the bootstrap index.php
, it appears several helper functions are based on the assumption of that public directory being there. In all honestly you really shouldn't be moving the public directory.
1) 我还没有找到在L5 中移动公共目录的工作方法。虽然您可以修改 bootstrap 中的某些内容index.php
,但似乎有几个辅助函数是基于该公共目录存在的假设。老实说,您真的不应该移动公共目录。
2) If your using MAMPthen you should be creating new vhosts for each project, each serving that projects public directory. Once created you access each project by your defined server name like this :
2) 如果您使用MAMP,那么您应该为每个项目创建新的虚拟主机,每个服务于该项目的公共目录。创建后,您可以通过定义的服务器名称访问每个项目,如下所示:
http://project1.dev
http://project2.dev
回答by piyush patel
It's possible to remove publicurl in laravel5. Follow these steps:
可以在 laravel5 中删除公共url。按着这些次序:
step 1.copy all file from publicand paste on rootdirectory
步骤 1. 从public复制所有文件并粘贴到根目录
step 2.open index.php
file
replace with
步骤2.打开index.php
文件替换为
require __DIR__.'/../bootstrap/autoload.php';
to
到
require __DIR__.'/bootstrap/autoload.php';
and
和
$app = require_once __DIR__.'/../bootstrap/app.php';
to
到
$app = require_once __DIR__.'/bootstrap/app.php';
And remove all cache and cookies.
并删除所有缓存和 cookie。