php Laravel 4 从 URL 中删除 public
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15586397/
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 4 removing public from URL
提问by Kevin Jolan
So, I'm running xampp on Windows. I'm currently trying to get familiar with the laravel framework. Now, when thats pointed out. How can i be able to access my laravel application/website direct within the root?
所以,我在 Windows 上运行 xampp。我目前正在尝试熟悉 Laravel 框架。现在,当那被指出时。我怎样才能直接在根目录中访问我的 Laravel 应用程序/网站?
Example,
例子,
- What I'm doing now is: localhost/laravel/public/about (to see the about page)
- What i want to do is: localhost/laravel/about
- 我现在正在做的是:localhost/laravel/public/about(查看关于页面)
- 我想做的是:localhost/laravel/about
Any good solutions for this? do i need to add a .htacess file on the root folder of laravel? (not the public one).
对此有什么好的解决方案吗?我需要在 Laravel 的根文件夹中添加一个 .htacess 文件吗?(不是公众号)。
Any suggestions?
有什么建议?
回答by Nirmal Garia
Easiest way is create .htaccessfile in your Laravel root with following content:
最简单的方法是.htaccess在 Laravel 根目录中创建文件,内容如下:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
It should be redirected easily.
它应该很容易重定向。
Reference:https://coderwall.com/p/erbaig/laravel-s-htaccess-to-remove-public-from-url
参考:https : //coderwall.com/p/erbaig/laravel-s-htaccess-to-remove-public-from-url
回答by HomeSlice
Here's how I did it.
这是我如何做到的。
- Edit your Windows Host file - C:\Windows\System32\drivers\etc\hosts
- Edit the Apache vhosts file - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
- Add an htaccess file to the laravel/public folder (if its not already there)
- Restart Xampp apache server
- 编辑您的 Windows 主机文件 - C:\Windows\System32\drivers\etc\hosts
- 编辑 Apache vhosts 文件 - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
- 将 htaccess 文件添加到 laravel/public 文件夹(如果它还没有)
- 重启 Xampp apache 服务器
Windows can be a real PITA when trying to edit the Hosts file because of the User Account Control. Since I work on all kinds of small hobby projects, I have to edit this file all the time so this is what I do.
由于用户帐户控制,当尝试编辑主机文件时,Windows 可能是一个真正的 PITA。由于我从事各种小型业余爱好项目,因此我必须一直编辑此文件,所以这就是我所做的。
- Install PSPad. It loads really fast and you can bookmark files for easy loading/editing. Sublime Text also works well if you load the two files I mentioned above and save the workspace as a new project.
- Right-click on the PSPad (or other editor) program shortcut and choose 'Run as Administrator'. You cannot save changes to the Hosts file unless you do this.
- Open the Windows Host file in the editor. This file does not have a file extension, so you have to choose "All Files" in the File Open dialog to even see the file.
At the bottom of the file, add this:
127.0.0.1 laravel.devThis tells Windows to point the web browser to localhost whenever you enter laravel.dev in the browser's address bar.
- Save the file.
- Open the xampp Apache httpd-vhosts.conf file.
At the bottom of the file, add this: (I am assuming xampp is installed at the root of the D: drive)
<VirtualHost *:80> ServerName laravel.dev DocumentRoot "D:/xampp/htdocs/laravel/public" <Directory "D:/xampp/htdocs/laravel/public"> </Directory> </VirtualHost>Add an htaccess file to your laravel/public folder (if its not already there). I think the default htaccess file that comes with L4 looks like this:
Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]- Restart your xampp apache server.
- Open a web browser and type in the address bar -
http://laravel.dev - That will take you to the index.php file in the "public" folder.
- To get to the About page, I think the address would be
http://laravel.dev/about
- 安装 PSPad。它加载速度非常快,您可以为文件添加书签以便于加载/编辑。如果你加载我上面提到的两个文件并将工作区保存为一个新项目,Sublime Text 也能很好地工作。
- 右键单击 PSPad(或其他编辑器)程序快捷方式,然后选择“以管理员身份运行”。除非您这样做,否则您无法保存对 Hosts 文件的更改。
- 在编辑器中打开 Windows 主机文件。该文件没有文件扩展名,因此您必须在“文件打开”对话框中选择“所有文件”才能查看该文件。
在文件底部,添加以下内容:
127.0.0.1 laravel.dev这告诉 Windows 每当您在浏览器的地址栏中输入 laravel.dev 时,将 Web 浏览器指向 localhost。
- 保存文件。
- 打开 xampp Apache httpd-vhosts.conf 文件。
在文件底部,添加:(我假设 xampp 安装在 D: 驱动器的根目录下)
<VirtualHost *:80> ServerName laravel.dev DocumentRoot "D:/xampp/htdocs/laravel/public" <Directory "D:/xampp/htdocs/laravel/public"> </Directory> </VirtualHost>将 htaccess 文件添加到您的 laravel/public 文件夹(如果它还没有)。我认为 L4 附带的默认 htaccess 文件如下所示:
Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]- 重新启动您的 xampp apache 服务器。
- 打开网页浏览器并在地址栏中输入 -
http://laravel.dev - 这将带您进入“public”文件夹中的 index.php 文件。
- 要进入关于页面,我认为地址是
http://laravel.dev/about
回答by gan
Move the contents of the /publicfolder down a level.
将/public文件夹的内容向下移动一个级别。
You'll need to update the include lines in index.php to point to the correct location. (if it's down a level, remove the '../').
您需要更新 index.php 中的包含行以指向正确的位置。(如果它下降了一个级别,请删除“../”)。
回答by Wasim A.
BEST Approch: I will not recommend removing public, instead on local computer create a virtual host point to public directory and on remote hosting change public to public_html and point your domain to this directory. Reason, your whole laravel code will be secure because its one level down to your public directory :)
最佳方法:我不建议删除 public,而是在本地计算机上创建一个指向 public 目录的虚拟主机,并在远程主机上将 public 更改为 public_html 并将您的域指向此目录。原因,您的整个 Laravel 代码将是安全的,因为它的下一级到您的公共目录:)
METHOD 1:I just rename server.php to index.php and it works
方法 1:我只是将 server.php 重命名为 index.php 就可以了
METHOD 2:
方法二:
Here is my Directory Structure,
这是我的目录结构,
/laravel/
... app
... bootstrap
... public
... etc
Follow these easy steps
遵循这些简单的步骤
- move all files from public directory to root /laravel/
- now, no need of public directory, so optionally you can remove it now
- now open index.php and make following replacements
- 将所有文件从公共目录移动到根目录 /laravel/
- 现在,不需要公共目录,因此您可以选择立即删除它
- 现在打开 index.php 并进行以下替换
require DIR.'/../bootstrap/autoload.php';
需要目录.'/../bootstrap/autoload.php';
to
到
require DIR.'/bootstrap/autoload.php';
需要目录.'/bootstrap/autoload.php';
and
和
$app = require_once DIR.'/../bootstrap/start.php';
$app = require_once DIR.'/../bootstrap/start.php';
to
到
$app = require_once DIR.'/bootstrap/start.php';
$app = require_once DIR.'/bootstrap/start.php';
- now open bootstrap/paths.php and change public directory path:
- 现在打开 bootstrap/paths.php 并更改公共目录路径:
'public' => DIR.'/../public',
'public' => DIR.'/../public',
to
到
'public' => DIR.'/..',
'public' => DIR.'/..',
and that's it, now try http:// localhost/laravel/
就是这样,现在尝试 http://localhost/laravel/
回答by Gaurav Gupta
Set you document root for apache to the public folder, and not the laravel folder. This is the simplest technique and recommended for production environments.
将 apache 的文档根目录设置为 public 文件夹,而不是 laravel 文件夹。这是最简单的技术,推荐用于生产环境。
回答by X-Coder
I'm using L5, This works for me fine:
我正在使用 L5,这对我来说很好用:
- Rename the server.php in the your Laravel root folder to index.php
- copy the .htaccess file from /public directory to your Laravel root folder.
- 将 Laravel 根文件夹中的 server.php 重命名为 index.php
- 将 .htaccess 文件从 /public 目录复制到 Laravel 根文件夹。
-- Thatz it!!!
——那就对了!!!
回答by Lead Developer
Add following code to htaccess file. It may helps you.
将以下代码添加到 htaccess 文件中。它可能会帮助你。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
回答by Marnix
I've been struggling with this problem too but i've found a simple solution that only requires you to create another .htaccess at the root of your application.
我也一直在努力解决这个问题,但我找到了一个简单的解决方案,只需要您在应用程序的根目录下创建另一个 .htaccess。
The .htaccess itself should contain this:
.htaccess 本身应该包含以下内容:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^{yoursite}.{extension} [NC]
RewriteRule (.*) http://www.{yoursite}.{extension}/ [R=301,L]
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/ [L]
The complete system keeps working but it redirects to the /public folder.
整个系统继续工作,但它重定向到 /public 文件夹。
This is how I solved the problem for myself.
这就是我为自己解决问题的方式。
Hope it helps!
希望能帮助到你!
Cheers.
干杯。
回答by Vishal Tarkar
Add following code in your .htaccess (if not exist create a .htaccess on laravel root directory)
在您的 .htaccess 中添加以下代码(如果不存在,请在 Laravel 根目录上创建一个 .htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
Source : http://tutsnare.com/remove-public-from-url-laravel/
来源:http: //tutsnare.com/remove-public-from-url-laravel/
at Source you also get another method to do same.
在 Source 上,您还可以使用另一种方法来做同样的事情。
Update : Preferred way to do it is make change in directory structure which explain in source URL.
更新:首选方法是更改在源 URL 中解释的目录结构。
回答by ddw147
just in simple step i did in laravel 5 make .htaccess like this in laravel folder
只是我在laravel 5中做的简单步骤在laravel文件夹中像这样制作.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ ./index.php [L]
</IfModule>
then rename your server.phpto index.php
然后将您的重命名server.php为index.php
that it it will work
它会起作用
or if you just doing local development
或者如果你只是做本地开发
run this comman php artisan serve
运行这个命令 php artisan serve
it will start local server at localhost:8000(port may vary)
它将在localhost:8000(端口可能会有所不同)启动本地服务器

