如何在Laravel生成的URL中去除"public/index.php"?

105
我需要在Laravel中从生成的URL中移除index.phppublic/index.php,通常路径是localhost/public/index.php/someWordForRoute,应该变成localhost/someWordForRoute.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

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

我怎么能改变那个?


7
在Laravel中使用漂亮的URL,确保启用了mod_rewrite - Kryten
1
https://dev59.com/cWEi5IYBdhLWcg3wFoy9#21662885 - The Alpha
2
这是另一种解决方法,当其他解决方法无效时可尝试使用:http://www.dev-metal.com/enable-mod_rewrite-ubuntu-14-04-lts/ - TuGordoBello
你也可以像这样移动文件:https://youtu.be/ybJYyU5FPv4 - MD Singh
1
重新开放投票者:这个问题从根本上是错误的,并且源于网络服务器的错误配置。唯一正确的解决方案是将网络服务器的文档根目录设置为公共目录。这里所有得票最高的答案都会带来潜在的安全问题并鼓励不良实践,这就是为什么我将其标记为一个重复问题的原因,而那个问题的最佳答案才是正确的解决方案。 - miken32
显示剩余3条评论
19个回答

0
希望这能像对我一样有所帮助。
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

0
 RewriteEngine on

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

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

在 .htaccess 文件中使用此代码,并重新启动服务器


0

我刚刚为一个项目安装了Laravel 5,根目录下有一个名为server.php的文件。

将其更改为index.php即可正常工作,或在终端中输入:

$cp server.php index.php

0

我已经启用了“mod_rewrite”,并复制了该页面上的代码,但仍然无法正常工作。 - TuGordoBello
1
确保在Apache中,您的路径设置为应用程序的public文件夹。 - Kisuka
ServerAdmin webmaster@localhost DocumentRoot /home/web-apps/myproject/public - TuGordoBello
你在启用mod_rewrite后重新启动了Apache吗? - Kisuka
我有一个名为“用户”的文件夹,里面存放着照片。同时我还有一个名为“用户”的刀版文件,当我在URL中写入“myproject.com/user”时,会显示文件夹中的图片。 - TuGordoBello

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

0

请注意

使用Docker提供Laravel项目时,您不需要执行任何操作。只有在您的网站根目录(或更常见的是public_html目录)是Laravel项目时才使用此选项(在使用Docker时不是这种情况)。

不要这样做!

您真的不应该将Laravel根目录中的server.php重命名为index.php,并将/public目录中的.htaccess文件复制到Laravel根目录中!!!这样每个人都可以访问一些您的文件(例如.env)。自己试试吧,您不想那样!

要这样做:

相反,您应该像这样在根目录中创建一个.htaccess文件:

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

这将悄悄地将所有基础URI重写为/public文件夹。甚至包括所有标题,例如HTTP授权头和所有可选的URI参数也将默默地传递到/public文件夹中。
就这样。

抄袭自https://dev59.com/aV4c5IYBdhLWcg3wD2rC#53516975 - miken32

0
如果您使用IIS Windows服务器,请在根文件夹中添加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>

如果你使用.htaccess,请使用以下代码:

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

0
Nginx配置,您可以使用以下等效配置。
location / {
if (!-e $request_filename) {
    rewrite ^(.*)$ /public/$1 last;
}

}


-3
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/(public)/(index.php)/$ $1/$4 [L]
</IfModule>

3
请提供更多关于您解决方案的说明。 - Naruto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接