php - 在共享主机上发布 Laravel 8.0 (.htaccess?)
问题描述
我正在使用共享托管 Apache cPanel 服务器在 GoDaddy 上安装新的 Laravel 8 项目。
在将项目目录移动到 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);
任何帮助我解决这个问题将不胜感激。
解决方案
推荐阅读
- react-native - 反应原生 Firebase 消息:发布到 Google Play 商店后未收到推送通知
- awk - 使用 awk 替换和添加文本
- c++ - 编译的 ncnn 库未在 Windows 10 代码块 mingw64 上链接
- php - 如何从 php 连接到 PostgreSQL 数据库?
- java - java.lang.reflect.InaccessibleObjectException:无法使字段私有最终瞬态 java.net.InetSocketAddress
- android - Android 11 媒体播放器不工作,Android SDK 30
- javascript - 使用 array.push() 方法在 javascript 中获取空数组
- python - 将输出保存到多处理进程之间的变量
- google-chrome - 在 Chrome 或 Firefox 中禁用跨域框架
- java - 有没有办法将日志日志级别设置为高于 TRACE?