【问题标题】:Laravel showing directory file instead of redirecting to public/index.phpLaravel 显示目录文件而不是重定向到 public/index.php
【发布时间】:2018-01-22 15:18:17
【问题描述】:

我对 Laravel 有点陌生,我正在尝试做下一件事:

我有一个运行 Apache 和 PHP 的 Centos 7,我在上面运行着不同的 Web 应用程序。

我在文件夹下的/var/www/html 中有所有应用程序,index.php 将它们全部链接起来。现在我安装了 composer Laravel 并在 /var/www/html/play/play1 添加了一个新的 Laravel 项目。

问题是当我尝试访问https://example.com/play/play1 时,我应该得到 Laravel 索引页面(public/index.php 下的那个)正确吗?

相反,我得到一个包含所有 Laravel 项目文件的目录。 如果我访问https://example.com/play/play1/public,我可以看到索引页面。

我已经搜索过了,我检查了AllowOverwrite all 是否存在,mod_rewrite 是否打开。在公用文件夹中有一个.htaccess 文件。

有哪位大神能帮帮我吗?

【问题讨论】:

  • 听起来 .htaccess 没有被读取,如果您访问 domain.com/index.php 网站加载正确吗?

标签: laravel .htaccess centos7


【解决方案1】:

在项目文件夹的根目录(您可以在其中看到 app/resources 文件夹等)创建一个 .htaccess 文件并粘贴以下内容:

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

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

        # 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]

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

【讨论】:

    【解决方案2】:

    Laravel 的根目录不应该是公开访问的目录 - 你必须指向 public/ 目录(index.php.htaccess 所在的位置)。

    所以在你的服务器配置中http://example.com/play/play1 应该指向var/www/html/play/play1/public

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 2019-12-27
      • 2014-01-14
      • 2020-10-31
      • 1970-01-01
      • 2015-09-30
      相关资源
      最近更新 更多