laravel - 在服务器中部署Laravel项目而不更改公共(public)文件夹路径

标签 laravel deployment laravel-8 liveserver

我找到了 Laravel 部署的解决方案
我们大多数人都卡在服务器中的laravel部署上,大多数原因是更改了公共(public)路径并修改了server.php

请在下面找到答案:-

最佳答案

只需更改 .htaccess 即可解决问题:

更改根目录中的两个文件 1..htaccess, 2..index.php

并更改公共(public)文件夹中的两个文件1..htaccess、2..index.php

Root Folder

.htaccess

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

RewriteEngine On

# Handle Authorization Header
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 ^ index.php

Root Folder

index.php

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9de9fce4f1f2efddf1fceffcebf8f1b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>>
 */

$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';

Public Folder

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

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Public Folder

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($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
}

/*
|--------------------------------------------------------------------------
| 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 __DIR__.'/../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 __DIR__.'/../bootstrap/app.php';

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

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

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

找到解决方案并欢呼... 快乐编码..

关于laravel - 在服务器中部署Laravel项目而不更改公共(public)文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72784611/

相关文章:

PHP Laravel 检查数组是否为空

asp.net-mvc-3 - SQL CE 部署 - AppHarbor - 无法加载 SQL Server Compact 的 native 组件

java - 如何在不停止服务器的情况下消除JBoss中 explode 的 war ?

php - Laravel 8 |登录后根据角色重定向

php - Laravel 8 分页无法正确加载

mysql - 使用 laravel 创建外键会出现错误 : Cannot add foreign key constraint

laravel - 重命名 Laravel 5 中的索引

php - laravel 如何在 Eloquent 中使用力指数

sharepoint - 将更改应用到 SharePoint 应用程序的最佳实践

laravel - Laravel-Livewire 如何触发事件并监听它们?