laravel public_html 文件无法在共享主机中加载
laravel public_html files fail to load in shared hosting
我的 laravel (v5.8) 项目在 localhost 中运行良好,但在我将其上传到 cPanel
之后,所有图像和 css 以及 public 中的 JS 文件html 无法在浏览器中加载并在浏览器控制台中显示错误,如:
"Failed to load our-approach.png:1 resource: the server responded with
a status of 404 ()"
"Failed to load resource: the main.css:1
server responded with a status of 500 ()"
虽然框架本身加载正常并且没有显示错误。
我将 PHP 更新为 7.4
并关注了几乎所有的 youtube 和 Whosebug 线程,但没有任何效果。
index.php修改为:
require __DIR__.'/../laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
AppServiceProvider.php修改
public function register()
{
$this -> app -> bind('path.public', function()
{
return base_path('public_html');
});
}
我在 cPanel 中的项目结构:
|-- bin
|-- 缓存
|-- laravel (没有public文件夹的项目文件)
|-- 应用程序
|-- bootstrap
|-- 配置
.env
server.php
...和其余文件
|-- 等等
|-- 日志
|-- 邮件
|-- public_html
|-- css
|-- js
|-- img
.htaccess
index.php
...其余文件
我的 htaccess 文件在 public_html 中:
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
您需要更改 .htaccess 文件。
添加这些行
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.*)$ public/ [L]
</IfModule>
但是请在更改 htaccess 文件之前进行备份
显然问题出在文件权限上,我将文件夹权限设置为 755,文件权限设置为 644,一切正常
我的 laravel (v5.8) 项目在 localhost 中运行良好,但在我将其上传到 cPanel
之后,所有图像和 css 以及 public 中的 JS 文件html 无法在浏览器中加载并在浏览器控制台中显示错误,如:
"Failed to load our-approach.png:1 resource: the server responded with a status of 404 ()"
"Failed to load resource: the main.css:1 server responded with a status of 500 ()"
虽然框架本身加载正常并且没有显示错误。
我将 PHP 更新为 7.4
并关注了几乎所有的 youtube 和 Whosebug 线程,但没有任何效果。
index.php修改为:
require __DIR__.'/../laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
AppServiceProvider.php修改
public function register()
{
$this -> app -> bind('path.public', function()
{
return base_path('public_html');
});
}
我在 cPanel 中的项目结构:
|-- bin
|-- 缓存
|-- laravel (没有public文件夹的项目文件)
|-- 应用程序
|-- bootstrap
|-- 配置
.env
server.php
...和其余文件
|-- 等等
|-- 日志
|-- 邮件
|-- public_html
|-- css
|-- js
|-- img
.htaccess
index.php
...其余文件
我的 htaccess 文件在 public_html 中:
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
您需要更改 .htaccess 文件。
添加这些行
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.*)$ public/ [L]
</IfModule>
但是请在更改 htaccess 文件之前进行备份
显然问题出在文件权限上,我将文件夹权限设置为 755,文件权限设置为 644,一切正常