如何删除 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:59:37  来源:igfitidea点击:

How Can I Remove “public/index.php” in the URL Generated Laravel?

php.htaccesslaravellaravel-4laravel-routing

提问by TuGordoBello

I need to remove index.phpor public/index.phpfrom the generated URL in Laravel; commonly path is localhost/public/index.php/someWordForRoute, It should be something like localhost/someWordForRoute.

我需要从 Laravel 中生成的 URL 中删除 index.phppublic/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 .htaccessfile your Laravel root directory if it does not exists already. (Normally it is under your public_htmlfolder)

如果它不存在,请在 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: enter image description here

接下来,将所有内容从公共目录中移到根文件夹中。它应该会产生与此类似的结果: 在此处输入图片说明

After that, all we have to do is edit the locations in the laravel_code/bootstrap/paths.phpfile and the index.phpfile.

之后,我们所要做的就是编辑laravel_code/bootstrap/paths.php文件和index.php文件中的位置。

In laravel_code/bootstrap/paths.phpfind 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';

Source: How to remove /public/ from URL in Laravel

来源:如何从 Laravel 的 URL 中删除 /public/

回答by Tejas Jadhav

I tried this on Laravel 4.2

我在 Laravel 4.2 上试过这个

Rename the server.phpin the your Laravel root folder to index.phpand copy the .htaccessfile from /publicdirectory to your Laravel root folder.

server.phpLaravel 根文件夹中的重命名为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-enableddirectory and see whether the rewrite.loadis 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.phpwe 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 .htaccesscompletely if you can,and use a Directorytag inside a VirtualHosttag. That being said, the rest of this is going to assume you are sticking with the .htaccessroute for whatever reason.

我之所以首先提到这一点,是因为.htaccess如果可以,最好完全摆脱,并在Directory标签中使用VirtualHost标签。话虽如此,其余的将假设您.htaccess出于任何原因坚持路线。

To break this down, a couple things could be happening:

为了解决这个问题,可能会发生一些事情:

  1. I note, perhaps pointlessly, that your file is named .htaccesin 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.

  2. 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. Change

    AllowOverride 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 .htaccessfor rewriting. Instead, do it for the folders you need to add .htaccessto.

  3. Do you have mod_rewriteenabled? 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 enable mod_rewrite: How to enable mod_rewrite for Apache 2.2

  1. 我注意到,也许毫无意义,您的文件已.htacces在您的问题中命名。我的猜测是这是一个错字,但万一你忽略了它,它实际上只是错过了第二个 s,这正是一种小错误,它会让我把头撞在墙上寻找更具挑战性的东西.

  2. 您的服务器不能允许.htaccess。要检查这一点,请转到您的 Apache 配置文件。在现代 Apache 构建中,这通常不在一个配置文件中,因此请确保您使用的是指向您的应用程序的配置文件,无论它存在于何处。改变

    AllowOverride none
    

    AllowOverride all
    

    作为后续,Apache 文档还建议,出于安全目的,如果您要.htaccess进行重写,则不要对每个文件夹都执行此操作。相反,请为您需要添加.htaccess到的文件夹执行此操作。

  3. mod_rewrite启用了吗?这可能会导致问题。我注意到你在顶部有标签<IfModule mod_rewrite.c>。尝试注释掉这些标签并重新启动 Apache。如果出现错误,则可能是因为您需要启用mod_rewriteHow 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:

  1. Rename server.phpin your Laravel root folder to index.php
  2. Copy the .htaccessfile from /publicdirectory to your Laravel root folder.
  1. server.php在 Laravel 根文件夹中重命名为index.php
  2. .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 .htaccessfile 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 已启用,并且您的主路径设置为应用程序的公共目录。