【问题标题】:Routing fail on Laravel app deployed on subfolder部署在子文件夹上的 Laravel 应用程序路由失败
【发布时间】:2019-12-18 17:00:57
【问题描述】:

好的,这就是正在发生的事情。

我已经创建了一个小型 laravel 应用程序(实际上,它还处于开发阶段)。

当我将它上传到服务器(通过 cPanel 管理的共享主机)时,我已将它部署到主网站内的子文件夹中。

服务器的 public_html 文件夹中有主网站(一个 WordPress),所以我创建了一个子文件夹来放置我的应用程序。

现在,当您请求 http://oursite.com 时,您将访问 Wordpress 站点。 当您请求 http://oursite.com/laravelapp 时,您会到达 laravel 应用程序的初始页面。

但是,当我尝试将任何页面浏览到应用程序中时,路由会将我返回到根 Web 文件夹内的路由。

例如,如果我按下登录按钮,http://oursite.com/laravelapp/login 的请愿会重定向到http://oursite.com/login,显然页面加载失败

有趣的是,如果我直接访问公共页面,它似乎工作正常。

如果我直接请求http://oursite.com/laravelapp/public/index.php 并按下相同的登录按钮,则会向http://oursite.com/laravelapp/public/index.php/login 发出请求,并且可以正常工作!!甚至 JS 和 CSS 链接都可以根据该请求正确检测到!!

我怀疑这可能是我现在看不到的非常愚蠢的事情,因为我太累了,但我非常感谢任何关于此事的见解。

我的 laravelapp 文件夹中的 index.php 如下:

    <?php

  /**
   * Laravel - A PHP Framework For Web Artisans
   *
   * @package  Laravel
   * @author   Taylor Otwell <taylor@laravel.com>
   */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';

这是 /laravelapp/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>

也许我可以使用 myLaravelApp.oursite.com 之类的子域来解决此问题?

【问题讨论】:

  • 公用文件夹中的.htaccess 文件怎么样?
  • @Shobi 我已将其附加到问题中。你看到那里有什么东西可以解决问题吗?
  • 您是否在配置文件中设置了站点 URL?
  • 我的 .env 文件上的@GBWDev APP_URL 指向oursite.com/laravelapp,如果这是您的要求

标签: laravel routing cpanel


【解决方案1】:

好的。

我修复了将 laravelApp/public 上的 .htaccess 复制到 laravelApp 的问题。

似乎根文件夹需要某种类型的重定向,这就像一个魅力。

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2022-12-18
    • 1970-01-01
    • 2011-12-07
    • 2014-06-27
    • 2020-02-17
    • 2021-10-31
    • 2011-05-02
    • 2015-05-14
    相关资源
    最近更新 更多