首页 > 解决方案 > 在共享主机上发布 Laravel 8.0 (.htaccess?)

问题描述

我正在使用共享托管 Apache cPanel 服务器在 GoDaddy 上安装新的 Laravel 8 项目。

使用了此演练: https ://medium.com/backenders-club/how-to-host-a-laravel-project-on-a-shared-hosting-via-cpanel-d955d32c528e

在将项目目录移动到 cPanel 根目录,并将我的 Laravel /public 文件移动到 cPanel /public_html 目录后,我已经启动了服务器并能够使用我的网络路由访问索引页面。

Route Used: 
Route::get('/', 'App\Http\Controllers\Controller@Index');

Page Visited: 
www.mywebsite.com/

当使用错误/未命名的路由(即www.mywebsite.com/unnamed_route)时,我将被重定向到 Laravel 的默认 404 页面,并按预期使用漂亮的 URL。

但是,当我向我的网络路由文件添加新路由并导航到应该是该路由的漂亮 URL 时,我被重定向到另一个 404 页面,该页面重定向/显示到我的索引文件的绝对路径,包括用户名我的共享主机。

New Route: 
Route::get('/test', 'App\Http\Controllers\Controller@test');

Browse to: 
www.mywebsite.com/test

URL Displayed: 
www.mywebsite.com/home/myusername/myprojectname/index.php

我假设这是一个简单的 .htaccess 问题,但是我不熟悉 .htaccess 配置。

我还想指出,当我访问www.mywebsite.com/index.php/test时,我被 Laravel 路由定向到正确的页面。

目录结构:

| cPanel root <br/>
|| myproject <br/>
||| Laravel project files (.env, artisan, bootstrap/, storage/, routes/, resources/, etc...) <br/>
|| public_html <br/>
||| .htaccess <br/> 
||| index.php <br/>

这是我的 index.php 文件:

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists('/home/myusername/myprojectname/storage/framework/maintenance.php')) {
    require '/home/myusername/myprojectname/storage/framework/maintenance.php';
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require '/home/myusername/myprojectname/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

$app = require_once '/home/myusername/myprojectname/bootstrap/app.php';

$kernel = $app->make(Kernel::class);

还有我的 .htaccess 文件:

Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^.*$ ./index.php


$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

$kernel->terminate($request, $response);

任何帮助我解决这个问题将不胜感激。

标签: phplaravel.htaccessweb-deploymentlaravel-8

解决方案


推荐阅读