简体   繁体   English

如何删除URL生成的Laravel中的“public/index.php”?

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

I need to remove index.php or public/index.php from the generated URL in Laravel;我需要从 Laravel 中生成的 URL 中删除index.phppublic/index.php index.php; commonly path is localhost/public/index.php/someWordForRoute , It should be something like localhost/someWordForRoute.通常路径是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 ^(.*)/$ /$1 [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?我该如何改变它?

Option 1: Use .htaccess选项 1:使用 .htaccess

If it isn't already there, create an .htaccess file in the Laravel root directory.如果它不存在,请在 Laravel 根目录中创建一个 .htaccess 文件。 Create a .htaccess file your Laravel root directory if it does not exists already.如果 Laravel 根目录不存在,则创建一个.htaccess文件。 (Normally it is under your public_html folder) (通常它在您的public_html文件夹下)

Edit the .htaccess file so that it contains the following code:编辑 .htaccess 文件,使其包含以下代码:

<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/$1 [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 ,找到以下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/

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.将 Laravel 根文件夹中的server.php重命名为index.php并将.htaccess文件从/public目录复制到 Laravel 根文件夹。

I hope it works我希望它有效

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/$1    [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=$1 [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]

1) Check whether mod_rewrite is enabled. 1) 检查是否启用了 mod_rewrite。 Go to /etc/apache2/mods-enabled directory and see whether the rewrite.load is available.进入/etc/apache2/mods-enabled目录,查看rewrite.load是否可用。 If not, enable it using the command sudo a2enmod rewrite .It will enable the rewrite module.如果没有,请使用命令sudo a2enmod rewrite启用它。它将启用重写模块。 If its already enabled, it will show the message Module rewrite already enabled .如果它已经启用,它将显示消息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 . 2)为公共目录创建一个虚拟主机,这样我们就可以像http://example.com/index.php那样使用,而不是给http://localhost/public/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) 3)然后创建一个.htaccess,它将从你的url中隐藏index.php,它应该在根目录(公共文件夹)内

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

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';在 Laravel 4 路径 2 中是$app = require_once __DIR__.'/bootstrap/start.php'; instead of $app = require_once __DIR__.'/bootstrap/app.php';/app.php而不是$app = require_once __DIR__.'/bootstrap/app.php';/app.php

That is it.这就对了。

For Laravel 4 & 5:对于 Laravel 4 和 5:

  1. Rename server.php in your Laravel root folder to index.php将 Laravel 根文件夹中的server.php重命名为index.php
  2. Copy the .htaccess file from /public directory to your Laravel root folder ..htaccess文件从/public目录复制到 Laravel根文件夹

That's it !!就是这样 !! :) :)

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?您是否有权访问您的 Apache 服务器配置? If so, check this out, from the docs:如果是这样,请从文档中查看:

In general, you should only use .htaccess files when you don't have access to the main server configuration file.通常,当您无权访问主服务器配置文件时,您应该只使用 .htaccess 文件。 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.例如,有一个常见的误解,即用户身份验证应该始终在 .htaccess 文件中完成,并且在最近几年,还有另一个误解,即 mod_rewrite 指令必须放在 .htaccess 文件中。 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.同样,mod_rewrite 指令在许多方面在主服务器配置中工作得更好。

... In general, use of .htaccess files should be avoided when possible. ... 一般来说,应尽可能避免使用 .htaccess 文件。 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.您考虑放入 .htaccess 文件的任何配置都可以在主服务器配置文件的一个部分中有效地进行。

Source: Apache documentation .来源: 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.我之所以首先提到这一点,是因为如果可以的话,最好完全摆脱.htaccess ,并在VirtualHost标签中使用Directory标签。 That being said, the rest of this is going to assume you are sticking with the .htaccess route for whatever reason.话虽如此,其余部分将假设您出于任何原因坚持使用.htaccess路线。

To break this down, a couple things could be happening:为了解决这个问题,可能会发生一些事情:

  1. I note, perhaps pointlessly, that your file is named .htacces in your question.我注意到,也许毫无意义,您的文件在您的问题中被命名为.htacces 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.我的猜测是这是一个错字,但万一你忽略了它,实际上它只是错过了第二个 s,这正是一种小错误,它会让我把头撞在墙上寻找更具挑战性的东西.

  2. Your server could not be allowing .htaccess .您的服务器.htaccess To check this, go to your Apache configuration file.要检查这一点,请转到您的 Apache 配置文件。 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.在现代 Apache 构建中,这通常不在一个配置文件中,因此请确保您使用的是指向您的应用程序的配置文件,无论它存在于何处。 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 .htaccess for rewriting.作为后续,Apache 文档还建议,出于安全目的,如果您打算使用.htaccess进行重写,则不要对每个文件夹都执行此操作。 Instead, do it for the folders you need to add .htaccess to.相反,为您需要添加.htaccess的文件夹执行此操作。

  3. Do you have mod_rewrite enabled?你有启用mod_rewrite吗? That could be causing the issues.这可能会导致问题。 I notice you have the tag at the top <IfModule mod_rewrite.c> .我注意到您在顶部有标签<IfModule mod_rewrite.c> Try commenting out those tags and restart Apache.尝试注释掉这些标签并重新启动 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如果出现错误,则可能是因为您需要启用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.如果可能的话,从不同的来源使用一个好的教程来做。

还要确保在编辑 .htaccess 文件后打开了 Rewrite Engline

sudo a2enmod rewrite

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 已启用,并且您的主路径设置为应用程序的公共目录。

You have to perform following steps to do this, which are as follows您必须执行以下步骤才能做到这一点,如下所示

  • Map your domain upto public folder of your project (ie /var/www/html/yourproject/public) (if using linux)将您的域映射到项目的公共文件夹(即 /var/www/html/yourproject/public)(如果使用 linux)

  • Go to your public folder edit your .htaccess file there转到您的公用文件夹,在那里编辑您的.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/*(.*) /$1$2 [R=301,NE,L]
</IfModule>


  • The last three rules are for if you are directly accessing any route without https , it protect that.最后三个规则适用于如果您直接访问任何没有https路由,它会保护它。

PLEASE NOTE请注意

when serving a Laravel project with Docker: you won't need to do any of this.使用 Docker 为 Laravel 项目提供服务时:您不需要执行任何这些操作。 Only use this option when your root (or more commonly: public_html) directory of your website is your Laravel project (this is not the case when you're using Docker).仅当您网站的根目录(或更常见的是:public_html)是您的 Laravel 项目时才使用此选项(当您使用 Docker 时,情况并非如此)。

DON'T!别!

YOU REALLY SHOULD NOT rename server.php in your Laravel root folder to index.php and copy the .htaccess file from the /public directory to your Laravel root folder!!!您真的不应该将 Laravel 根文件夹中的 server.php 重命名为 index.php 并将 .htaccess 文件从 /public 目录复制到 Laravel 根文件夹中!!!

This way everyone can access some of your files (.env for example).这样每个人都可以访问您的某些文件(例如 .env)。 Try it yourself.自己试试吧。 You don't want that!你不想那样!

DO

Instead, you should create an .htaccess file in your root like this:相反,您应该在您的根目录中创建一个 .htaccess 文件,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

This will silently rewrite all your base URIs to the /public folder.这将默默地将所有基本 URI 重写到 /public 文件夹。 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.甚至所有标头(例如 HTTP 授权标头)和所有可选的 URI 参数也将被静默传递到 /public 文件夹。

That's all就这样

create a new file with the name of .htaccess and add below lines will fix it.创建一个名为 .htaccess 的新文件并添加以下行将修复它。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

I am sure it will be fixed, as I faced this issue multiple times and fixed this way.我相信它会被修复,因为我多次遇到这个问题并以这种方式修复。

Thank You.谢谢你。

 RewriteEngine on

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 RewriteRule ^(.*)$ index.php/$1 [L]

use this in .htaccess and restart the server在 .htaccess 中使用它并重新启动服务器

Hope this helps like it did for me.希望这对我有帮助。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

将 Laravel 根文件夹中的server.php重命名为index.php并将.htaccess文件从/public目录复制到 Laravel 根文件夹。

I just installed Laravel 5 for a project and there is a file in the root called server.php .我刚刚为一个项目安装了 Laravel 5,根目录中有一个名为server.php的文件。

Change it to index.php and it works or type in terminal:将其更改为index.php并且它可以工作或在终端中输入:

$cp server.php index.php

I need to remove index.php or public/index.php from the generated URL in Laravel;我需要从Laravel中生成的URL中删除index.phppublic/index.php commonly path is localhost/public/index.php/someWordForRoute , It should be something like localhost/someWordForRoute.通常路径是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 ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php[L]

app/config/app.php app / config / app.php

'url' => 'http://localhost',

How can I change that?我该如何改变呢?

If you use IIS Windows Server add web.config file in root folder and put the below code :如果您使用 IIS Windows Server,请在根文件夹中添加web.config文件并放入以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{URL}" pattern="^system.*" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="/index.php/{R:1}" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{R:1}" pattern="^(index\.php|images|robots\.txt|css)" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

and if you use .htaccess use this one :如果你使用.htaccess使用这个:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/(public)/(index.php)/$ $1/$4 [L]
</IfModule>

As per Laravel 5.3 the above things will not work properly, but please be calm and follow below steps: 根据Laravel 5.3 ,上述内容将无法正常工作,但请保持冷静,并按照以下步骤操作:

Step 1. Go to inside of laravel project 步骤1.进入laravel项目内部
Step 2. Make one random directory/folder eg. 步骤2.制作一个随机目录/文件夹,例如。 laravelcode laravelcode
Step 3. Move all the folder and files(Except public folder) 步骤3.移动所有文件夹和文件( 公共文件夹除外)
Step 4. Move all the folder and files which are inside the public folder to the laravel project 步骤4.将公共文件夹内的所有文件夹和文件移动到laravel项目
Step 5. Now inside the laravel project there is index.php file, please open that and change the folder paths for bootstrap as per new path like previously it was "require DIR .'/../bootstrap/autoload.php';" 步骤5.现在在laravel项目中有index.php文件,请打开它并按照新路径更改bootstrap的文件夹路径,就像之前它是“require DIR 。”/ .. / bootstrap / autoload.php';“ now it will become to "require DIR .'/laravelcode/bootstrap/autoload.php';" 现在它将成为“要求DIR 。”/ laravelcode / bootstrap / autoload.php';“

That's it just run your url without public and index.php like below http://localhost/blog/ 这就是它运行你的url没有public和index.php,如下面http:// localhost / blog /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM