Laravel htaccess

17

我在本地搭建了一份 Laravel 的新安装。似乎存在htaccess或Apache设置的问题。我已经研究了几个小时并尝试了所有我找到的方法。

  • OSX Lion 10.7.5
  • MAMP 3.0.5
  • PHP 5.5.10
  • mod_rewrite已被加载。

我的开发服务器可以正常访问其他网站。这是我第一次尝试 Laravel 4。

当我访问位于 website.dev:8888/ 的欢迎页面时,会显示403 Forbidden。

Apache 给出以下错误提示:Directory index forbidden by Options directive。

这是我的 .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]
</IfModule>

以下是我采取的一些额外措施:

  • 在httpd.conf中将AllowOverride设置为All
  • 在httpd-vhosts.conf中添加了虚拟主机代码段
  • 验证hosts文件包含网站127.0.0.1 website.dev的行

我还尝试过在htaccess中使用我在文章中找到的各种行,并且每次更改配置文件时都重新启动了Apache。没有任何路由起作用。当我访问website.dev:8888/public时,我得到一个空白页面,没有错误。如果我访问我创建的路由,比如website.dev:8888/users,我会得到404未找到错误。

感谢你的帮助!


只是确认一下,“public”文件夹是你的网站根目录吗?(应该是的!) - MightyPork
不,我认为它不是。我该如何设置才能使其成为呢? - Alexnl
最简单的方法是将 httpd.conf 中的 DocumentRoot "/srv/httpd" 更改为 DocumentRoot "/srv/httpd/public" - MightyPork
10个回答

39

这个解决方案很好,对我来说是最好的解决方案。将此代码粘贴到根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
</IfModule>

2
这段代码来自哪里?(它实际上如何解决OP的具体问题?) - MrWhite
7
可以解释一下这个解决方案吗?特别是指将某些部分重定向到公共或服务器脚本,但似乎它确实能够工作。 - Janne
首先进入public/*,然后指向server.php。所以不需要在任何其他地方包含/public/ ;) - Mohammad H.
1
无论如何,谢谢,它对我完美地起作用了。希望有一天能够理解这段代码,但是今天复制粘贴就可以了。 - Juan Joya
@MohammadH。我没有URL中的“/public”问题。我的唯一问题是我的应用程序没有样式。我的CSS JS文件没有加载。我使用的是laravel 6。我已经尝试了这篇文章中的所有解决方案。 - Umair
显示剩余2条评论

19
在根路径下创建一个名为 .htaccess 的文件。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/ 

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



RewriteRule ^(.*)$ /public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

在公共目录中创建一个 .htaccess 文件

<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 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]
</IfModule>

如果您在public/index.php文件中进行了任何更改,请使用正确的路径进行更正,然后您的站点将上线。

这将解决什么问题?

  • Laravel项目的托管问题。
  • CSS在Laravel上不起作用。
  • Laravel网站无法工作。
  • Laravel网站加载具有domain/public/index.php。
  • Laravel项目没有正确重定向。

2
我爱你,伙计。找了3天的解决方案。 - Lucas De Lavra Pinto
我们也花了相当长的时间来解决这个问题,而这是唯一有效的解决方案。非常感谢您提供的详细说明! - user754730

7

答案

这是我发现可行的方法。删除 /public 目录中的 .htaccess 文件,然后将以下内容放入 Laravel 安装文档根目录。


澄清

如果您的 Laravel 应用程序安装在/public_html/laravel,则要找到 /public_html/laravel/public 并删除其中的 .htaccess 文件。然后,在 /public_html/laravel 目录中创建一个名为.htaccess 的新文件。


将下面的代码复制到新的.doc根目录下的 htaccess 文件中

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /public/$1 [L,R=301]

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

请注意:
正如Concordus应用这里回答的那样,请确保您的apache配置文件中未注释 Loadmodule mod_rewrite(请确保后面没有#符号)。
Concordus应用还指出,您应该将AllowOverride设置为适当的值。他们给出了以下示例。
<Directory "/some/absolute/path/htdocs">
    Options Indexes Includes FollowSymLinks MultiViews
    AllowOverride AuthConfig FileInfo
    Order allow,deny
    Allow from all
</Directory> 

请确保在更改了Apache配置文件后重新启动您的Apache服务器。

5
该框架附带了一个名为public/.htaccess的文件,用于允许不使用index.php的URL。如果您使用Apache来提供Laravel应用程序,请确保启用mod_rewrite模块。
如果附带的Laravel .htaccess文件在您的Apache安装中无法使用,请尝试使用此文件:
Options +FollowSymLinks
RewriteEngine On

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

5

已经接受的答案可以正常工作,但与Laravel Passport身份验证不兼容。

需要添加一些头部调整:

RewriteEngine On

RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

4
这个 .htaccess 文件将从您的网址中删除 /public/ ,并强制使用 https://。
将这个 .htaccess 文件放在您的根目录下,不要重命名 server.php 文件为 index.php。这个 .htaccess 文件会完成一切。
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTPS} !=on    
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteRule ^(.*)$ public/$1 [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

</IfModule>


如果您遇到服务器错误,您可能需要在“RewriteEngine On”之后将以下内容添加到.htaccess文件中: “RewriteBase /” 在Ionos/1&1网络托管上对我有效。 - ftw
@umair 有几件事情需要检查。
  1. 检查你的 APP_URL
  2. 确保你正在使用 {{ asset('/css/app.css') }} 这样的东西
  3. 检查你的网络选项卡以读取你得到了什么状态码。
- Pushkraj Jori
在我采用你的解决方案之前,我执行了ctrl + u,看到了这个。/public在url中重复出现了两次。请参见图像https://i.postimg.cc/R00GQkXQ/Untitled.jpg - Umair
@ftw 这是 Github 存储库的链接 https://github.com/cmate5614530/inventory-management - Umair
@umair 这是你的实际代码库吗?github.com/cmate5614530/inventory-management - Pushkraj Jori
显示剩余6条评论

3
请确保在 /wwwroot 和 /public 文件夹中都有 .htaccess 文件。 不过两个文件的内容是不同的: wwwroot/.htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/ 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

wwwroot/public/.htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On    
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

这在ionos托管中对我有效。 - meYnot
这仅适用于默认URL(localhost:8000),但不适用于调用其他路由,如localhost:8000/Mycontroller。我们需要再次包含public以使这些内容正常工作。 - Mak-Mak Basaya

1
在根目录下创建新的 .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
</IfModule>

并将此代码添加到public/.htaccess文件的末尾

    RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
    RewriteRule ^ /%1%2? [R=301,L,NE]

0

简单解决方案:将以下代码复制并粘贴到您的.htaccess文件中

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

-1
使用这个
RewriteEngine 开启 RewriteRule ^(.*)$ public/$1 [L]

1
这个看起来和其他几个现有的答案很相似,但是细节较少。这个有什么改进之处呢?为什么我们应该使用这个而不是其他的呢? - undefined

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