如何删除 Laravel 生成的 URL 中的“public/index.php”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23837933/
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
How Can I Remove “public/index.php” in the URL Generated Laravel?
提问by TuGordoBello
I need to remove index.php
or public/index.php
from the generated URL in Laravel; commonly path is localhost/public/index.php/someWordForRoute
, It should be something like localhost/someWordForRoute.
我需要从 Laravel 中生成的 URL 中删除 index.php
或public/index.php
;通常路径是localhost/public/index.php/someWordForRoute
,它应该是这样的localhost/someWordForRoute.
.htaccess
.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]
app/config/app.php
应用程序/配置/app.php
'url' => 'http://localhost',
How can I change that?
我怎样才能改变它?
回答by Luzan Baral
Option 1: Use .htaccess
选项 1:使用 .htaccess
If it isn't already there, create an .htaccess file in the Laravel root directory.
Create a .htaccess
file your Laravel root directory if it does not exists already. (Normally it is under your public_html
folder)
如果它不存在,请在 Laravel 根目录中创建一个 .htaccess 文件。.htaccess
如果 Laravel 根目录不存在,则创建一个文件。(通常它在您的public_html
文件夹下)
Edit the .htaccess file so that it contains the following code:
编辑 .htaccess 文件,使其包含以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/ [L]
</IfModule>
Now you should be able to access the website without the "/public/index.php/" part.
现在您应该能够在没有“/public/index.php/”部分的情况下访问该网站。
Option 2 : Move things in the '/public' directory to the root directory
选项 2:将“/public”目录中的内容移动到根目录
Make a new folder in your root directory and move all the files and folder except public folder. You can call it anything you want. I'll use "laravel_code".
在您的根目录中创建一个新文件夹并移动除公共文件夹之外的所有文件和文件夹。你可以随意称呼它。我将使用“laravel_code”。
Next, move everything out of the public directory and into the root folder. It should result in something somewhat similar to this:
接下来,将所有内容从公共目录中移到根文件夹中。它应该会产生与此类似的结果:
After that, all we have to do is edit the locations in the laravel_code/bootstrap/paths.php
file and the index.php
file.
之后,我们所要做的就是编辑laravel_code/bootstrap/paths.php
文件和index.php
文件中的位置。
In laravel_code/bootstrap/paths.php
find the following line of code:
在laravel_code/bootstrap/paths.php
找到以下代码行:
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../public',
And change them to:
并将它们更改为:
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../../',
In index.php
, find these lines:
在 中index.php
,找到这些行:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
And change them to:
并将它们更改为:
require __DIR__.'/laravel_code/bootstrap/autoload.php';
$app = require_once __DIR__.'/laravel_code/bootstrap/start.php';
回答by Tejas Jadhav
I tried this on Laravel 4.2
我在 Laravel 4.2 上试过这个
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.
将server.php
Laravel 根文件夹中的重命名为index.php
并将.htaccess
文件从/public
目录复制到 Laravel 根文件夹。
I hope it works
我希望它有效
回答by Ankit Vishwakarma
Don't get confused with other option the snippet below I am using and will be useful for you too. Put the below htacces in your root.
不要与我正在使用的以下代码段的其他选项混淆,这对您也很有用。将以下 htacces 放在您的根目录中。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/ [L]
</IfModule>
Go to your public directory and put another htaccess file with the code snippet below
转到您的公共目录并放置另一个带有以下代码片段的 htaccess 文件
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url= [PT,L]
</IfModule>
Its working... !!!
它的工作... !!!
Laravel Uses below .htaccess
Laravel 在 .htaccess 下面使用
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
回答by Khandad Niazi
HERE ARE SIMPLE STEPS TO REMOVE PUBLIC IN URL (Laravel 5)
以下是删除 PUBLIC IN URL 的简单步骤 (Laravel 5)
1: Copy all files form public folder and past them in laravel root folder
1:从公共文件夹复制所有文件并将它们粘贴到laravel根文件夹中
2: Open index.php and change
2:打开index.php并修改
From
从
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';
In laravel 4 path 2 is $app = require_once __DIR__.'/bootstrap/start.php';
instead of $app = require_once __DIR__.'/bootstrap/app.php';/app.php
在 laravel 4 路径 2 是$app = require_once __DIR__.'/bootstrap/start.php';
而不是$app = require_once __DIR__.'/bootstrap/app.php';/app.php
That is it.
这就对了。
回答by Orchid
1) Check whether mod_rewrite is enabled. Go to /etc/apache2/mods-enabled
directory and see whether the rewrite.load
is available. If not, enable it using the command sudo a2enmod rewrite
.It will enable the rewrite module. If its already enabled, it will show the message Module rewrite already enabled
.
2)Create a virtual host for public directory so that instead of giving http://localhost/public/index.php
we will be able to use like that http://example.com/index.php
. [hence public folder name will be hidded]
3)Then create a .htaccess which will hide the index.php from your url which shoul be inside the root directory (public folder)
1) 检查是否启用了 mod_rewrite。转到/etc/apache2/mods-enabled
目录并查看是否rewrite.load
可用。如果没有,请使用命令启用它sudo a2enmod rewrite
。它将启用重写模块。如果它已经启用,它将显示消息Module rewrite already enabled
。
2)为公共目录创建一个虚拟主机,这样http://localhost/public/index.php
我们就可以使用这样的http://example.com/index.php
. [因此公用文件夹名称将被
隐藏] 3)然后创建一个 .htaccess,它将从您的 url 中隐藏 index.php,它应该位于根目录(公用文件夹)内
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
回答by smcjones
This likely has very little to do with Laravel and everything to do with your Apache configs.
这可能与 Laravel 几乎没有关系,而与您的 Apache 配置有关。
Do you have access to your Apache server config? If so, check this out, from the docs:
您是否有权访问您的 Apache 服务器配置?如果是这样,请从文档中查看:
In general, you should only use .htaccess files when you don't have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things. Likewise, mod_rewrite directives work better, in many respects, in the main server configuration.
... In general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a section in your main server configuration file.
Source: Apache documentation.
通常,当您无权访问主服务器配置文件时,您应该只使用 .htaccess 文件。例如,存在一种常见的误解,即用户身份验证应该始终在 .htaccess 文件中完成,并且在最近几年,还有另一种误解,即 mod_rewrite 指令必须放在 .htaccess 文件中。这根本不是那么回事。您可以将用户身份验证配置放在主服务器配置中,这实际上是首选方式。同样,mod_rewrite 指令在许多方面在主服务器配置中工作得更好。
... 一般来说,应尽可能避免使用 .htaccess 文件。您考虑放入 .htaccess 文件的任何配置都可以在主服务器配置文件的一个部分中有效地进行。
来源:Apache 文档。
The reason why I mention this first is because it is best to get rid of .htaccess
completely if you can,and use a Directory
tag inside a VirtualHost
tag. That being said, the rest of this is going to assume you are sticking with the .htaccess
route for whatever reason.
我之所以首先提到这一点,是因为.htaccess
如果可以,最好完全摆脱,并在Directory
标签中使用VirtualHost
标签。话虽如此,其余的将假设您.htaccess
出于任何原因坚持路线。
To break this down, a couple things could be happening:
为了解决这个问题,可能会发生一些事情:
I note, perhaps pointlessly, that your file is named
.htacces
in your question. My guess is that it is a typo but on the off chance you overlooked it and it is in fact just missing the second s, this is exactly the sort of minor error that would leave me banging my head against the wall looking for something more challenging.Your server could not be allowing
.htaccess
. To check this, go to your Apache configuration file. In modern Apache builds this is usually not in one config file so make sure you're using the one that points to your app, wherever it exists. ChangeAllowOverride none
to
AllowOverride all
As a followup, the Apache documents also recommend that, for security purposes, you don't do this for every folder if you are going to go for
.htaccess
for rewriting. Instead, do it for the folders you need to add.htaccess
to.Do you have
mod_rewrite
enabled? That could be causing the issues. I notice you have the tag at the top<IfModule mod_rewrite.c>
. Try commenting out those tags and restart Apache. If you get an error then it is probably because you need to enablemod_rewrite
: How to enable mod_rewrite for Apache 2.2
我注意到,也许毫无意义,您的文件已
.htacces
在您的问题中命名。我的猜测是这是一个错字,但万一你忽略了它,它实际上只是错过了第二个 s,这正是一种小错误,它会让我把头撞在墙上寻找更具挑战性的东西.您的服务器不能允许
.htaccess
。要检查这一点,请转到您的 Apache 配置文件。在现代 Apache 构建中,这通常不在一个配置文件中,因此请确保您使用的是指向您的应用程序的配置文件,无论它存在于何处。改变AllowOverride none
到
AllowOverride all
作为后续,Apache 文档还建议,出于安全目的,如果您要
.htaccess
进行重写,则不要对每个文件夹都执行此操作。相反,请为您需要添加.htaccess
到的文件夹执行此操作。你
mod_rewrite
启用了吗?这可能会导致问题。我注意到你在顶部有标签<IfModule mod_rewrite.c>
。尝试注释掉这些标签并重新启动 Apache。如果出现错误,则可能是因为您需要启用mod_rewrite
: How to enable mod_rewrite for Apache 2.2
Hope this helps. I've named some of the most common issues but there are several others that could be more mundane or unique. Another suggestion on a dev server? Re-install Apache. If possible, do so from a different source with a good tutorial.
希望这可以帮助。我列举了一些最常见的问题,但还有其他几个可能更平凡或更独特。关于开发服务器的另一个建议?重新安装阿帕奇。如果可能,请从其他来源提供良好的教程。
回答by Abhijeet Navgire
For Laravel 4 & 5:
对于 Laravel 4 和 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 Shahrukh Anwar
You have to perform following steps to do this, which are as follows
您必须执行以下步骤才能做到这一点,如下所示
Map your domain upto public folder of your project (i.e. /var/www/html/yourproject/public) (if using linux)
Go to your public folder edit your
.htaccess
file there
将您的域映射到项目的公共文件夹(即 /var/www/html/yourproject/public)(如果使用 linux)
转到您的公用文件夹,在
.htaccess
那里编辑您的文件
AddHandler application/x-httpd-php72 .php
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non-http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Remove index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) / [R=301,NE,L]
</IfModule>
- The last three rules are for if you are directly accessing any route without
https
, it protect that.
- 最后三个规则适用于如果您直接访问任何没有 的路由
https
,它会保护它。
回答by Rafik Bari
Also make sure the Rewrite Engline is turned on after editing the .htaccess file
还要确保在编辑 .htaccess 文件后打开了 Rewrite Engline
sudo a2enmod rewrite
回答by Kisuka
Laravel ships with a pretty URL .htaccess already... you should be good to go out of the box... http://laravel.com/docs/installation#pretty-urls
Laravel 附带了一个漂亮的 URL .htaccess ......你应该开箱即用...... http://laravel.com/docs/installation#pretty-urls
Make sure mod_rewrite is enabled and that your home path is set to the public directory of your application.
确保 mod_rewrite 已启用,并且您的主路径设置为应用程序的公共目录。