【问题标题】:Remove public from URL in Laravel 9 using .htaccess on shared hosting/CPanel hosting在共享主机/CPanel 主机上使用 .htaccess 从 Laravel 9 中的 URL 中删除 public
【发布时间】:2023-02-08 15:35:32
【问题描述】:

我正在尝试在 Cpanel 共享主机上托管 Laravel 9 应用程序。但是,即使我尝试访问https://example.com/public,应用程序仍会返回以下错误。下面还有我的根目录下的 .htaccess 文件;它曾经与 Laravel 8 完美配合,但不再工作了。我的问题是我只想使用没有 /public/public.index.php 的域(例如 example.com)来访问我的应用程序。

内部服务器错误 500

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

【问题讨论】:

  • 您需要检查服务器的错误日志以了解此 500 错误的详细信息。您还有其他.htaccess 文件吗? (您应该在/public/.htaccess 处有另一个.htaccess 文件。“或/public.index.php” - 大概您的意思是/public/index.php

标签: php .htaccess cpanel shared-hosting laravel-9


【解决方案1】:
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

您缺少条件模式!^public,所以这个状况将始终成功并重复重写请求,可能导致重写循环(即 500 错误)。

它应该是这样的:

RewriteCond %{REQUEST_URI} !^/public($|/)
RewriteRule ^(.*)$ public/$1 [L]

但是,您还应该在 /public 子目录中有另一个带有 mod_rewrite 指令的 .htaccess 文件,该文件管理通过您的 Laravel 应用程序的路由,这应该可以防止重写循环。

【讨论】:

  • 我得到一个 404 not found 通过使用它。我也在使用 Laravel 9。知道可能是什么原因吗?
  • @MeV 根据您的配置,可能有多种原因。你需要问另一个问题。
【解决方案2】:

如何从 laravel 9 中的 url 中删除 public

  • 在根目录中创建一个 .htaccess 文件

  • 添加此代码

    重写引擎开启

    RewriteCond %{REQUEST_FILENAME} -d [或] RewriteCond %{REQUEST_FILENAME} -f 重写规则 ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (.w+$) [NC] 重写规则 ^(.*)$ public/$1

    RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f 重写规则 ^ server.php

  • 在根目录下创建server.php

  • 添加此代码

    $uri = 网址解码( 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(目录.'/public'.$uri)) { 返回假; }

    要求一次目录.'/public/index.php';

【讨论】:

    猜你喜欢
    • 2020-01-16
    • 2018-09-04
    • 2017-06-28
    • 2018-08-02
    • 2023-03-31
    • 1970-01-01
    • 2019-04-07
    • 2011-12-05
    • 2018-05-26
    相关资源
    最近更新 更多