Laravel htaccess

问题描述 投票:0回答:10

我已经在本地安装了新的 Laravel。 htaccess 或 Apache 设置似乎存在问题。我研究了几个小时并尝试了我读到的所有内容。

  • OSX Lion 10.7.5
  • MAMP 3.0.5
  • PHP 5.5.10
  • mod_rewrite 正在加载。

我的开发服务器可以与其他站点配合使用。这是我第一次尝试 Laravel 4。

我在欢迎页面上收到 403 Forbidden,该页面位于 website.dev:8888/

Apache 给我这个错误:Options 指令禁止目录索引

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

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

  • AllowOverride 在 httpd.conf 中设置为 All
  • 向 httpd-vhosts.conf 添加了虚拟主机代码部分
  • 已验证主机文件包含站点 127.0.0.1 website.dev 的行

我还尝试了在文章中找到的 htaccess 中的各种行,并且每次更改 conf 文件时我还重新启动了 apache。没有路线工作。当我访问 website.dev:8888/public 时,我得到一个空白页面,没有错误。如果我转到我创建的路由,例如 website.dev:8888/users,我会收到 404 未找到错误。

谢谢您的帮助!

.htaccess laravel-4 laravel-routing
10个回答
39
投票

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

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>

在 public 目录中创建一个 .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 项目未正确重定向

7
投票

回答

这就是我发现有效的方法。删除

.htaccess
目录中的
/public
文件,然后将以下内容放入 laravel 安装的文档根目录中。


澄清

因此,为了澄清,如果您的 Laravel 应用程序安装在

/public_html/laravel
,您将查看
/public_html/laravel/public
内部并删除那里的
.htaccess
文件。然后在
/public_html/laravel
目录中创建一个名为
.htaccess

的新文件

将以下代码复制到文档根目录中的新 .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应用程序他们的答案中指出的那样确保

Loadmodule mod_rewrite
未注释(确保您的apache配置文件中其后面没有
#
符号)

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 护照身份验证。

需要添加一些标题调整:

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 将从您的 URL 中删除 /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>


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>

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 文件末尾的 .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
投票

使用这个

重写引擎开启 RewriteRule ^(.*)$ 公共/$1 [L]
© www.soinside.com 2019 - 2024. All rights reserved.