如何使用 laravel 在共享托管中不使用 public 即可访问我的路线

问题描述 投票:0回答:3

如何使用 Laravel 在共享托管中不使用公共的情况下使我的路线可访问,我尝试使用此 .htaccess 但它不起作用

重写引擎开启 RewriteRule ^(.*)$ 公共/$1 [L]

我目前使用的是 Hostinger,我的文件夹结构是

->public_html ->laravel 文件

enter image description here

我的索引位于公共文件夹中

我尝试了这个 .htaccess 代码但不起作用 重写引擎开启 RewriteRule ^(.*)$ 公共/$1 [L]

我的索引位于公共文件夹中

laravel .htaccess hosting web-hosting shared-hosting
3个回答
2
投票

将公共文件夹下的

index.php
.htaccess
文件移至父文件夹。

不要忘记修复
index.php
文件中的路径。像这样:

// require __DIR__ . '/../vendor/autoload.php'; to...
require __DIR__ . '/vendor/autoload.php';

您的项目将在 public_html 中触发。


0
投票

这种情况的最佳实践是创建如下 htaccess :如何将所有请求重定向到 laravel 5 中的 public/ 文件夹

但是我们仍然有一个“不太推荐”的技巧来处理托管提供商的有限功能:

  1. 移动项目文件中的所有文件(“public”目录除外),并创建新的“private”文件夹。在此步骤中,“私人”和“公共”文件夹位于同一级别,如下所示:
- private
   - app
   - bootstrap 
   - config
   - database
   - ...
- public
   - index.php
   - your_assets_or_anything
   - ...
  1. 将所有“公共”目录内容移至根项目。在这一步中,目录结构将是这样的:
- private
   - app
   - bootstrap 
   - config
   - database
   - ...
- index.php
- your_assets_or_anything
- ...
  1. 最后打开当前根目录下的“index.php”文件,将所有
    /../
    替换为
    /private/
    。示例:
<?php

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

define('LARAVEL_START', microtime(true));
// ...
if (file_exists($maintenance = __DIR__.'/private/storage/framework/maintenance.php')) {
    require $maintenance;
}
// ...
require __DIR__.'/private/vendor/autoload.php';
// ...
$app = require_once __DIR__.'/private/bootstrap/app.php';

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

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

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

请记住,这个技巧存在安全问题。您需要保护所有私人文件不被公共访问。


0
投票

public_html中的.htaccess

中添加以下代码
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

public文件夹下的.htaccess中插入以下代码

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

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

这是重定向您的请求以使您的网站上线的最简单方法。

© www.soinside.com 2019 - 2024. All rights reserved.