我正在使用 yii2 高级项目,我只是将文件上传到托管中。不幸的是,我的网站没有加载资产文件 - 如 CSS 或 js。
可能有一些不好的路径,但我不确定。
有几个类似的错误:
GET http://myhosting.pl/frontend/web/css/site.css net::ERR_ABORTED 404 (Not Found)
该错误与此行有关:<link href="/frontend/web/css/site.css" rel="stylesheet">
我认为这可能是 .htaccess 或类似问题,因为我完全是处理托管文件的新手。我的 .htaccess 看起来像这样:
AddDefaultCharset UTF-8
Options FollowSymLinks
DirectoryIndex index.php index.html
RewriteEngine on
RewriteRule /\. - [L,F]
# define the app environment variable
RewriteCond %{REQUEST_URI} !^/((frontend|backend)/views|admin)
RewriteRule ^ - [E=APP:frontend]
RewriteCond %{REQUEST_URI} (?!^/backend/web)^/admin
RewriteRule ^ - [E=APP:backend]
# rewrite the URI of the frontend app
RewriteCond %{ENV:APP} =frontend
RewriteRule ^ frontend/web%{REQUEST_URI}
# if a directory or a file exists, use the request directly
RewriteCond %{ENV:APP} =frontend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule ^ frontend/web/index.php [L]
# redirect to the URL without a trailing slash (uncomment if necessary)
#RewriteRule ^admin/$ /admin [L,R=301]
# rewrite the URI of the backend app
RewriteCond %{ENV:APP} =backend
RewriteRule ^admin/?(.*)$ backend/web/$1
# if a directory or a file exists, use the request directly
RewriteCond %{ENV:APP} =backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule ^ backend/web/index.php [L]
# handle a directory trailing slash, redirect to the initial URI instead of the rewritten one
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301]
我只想补充一点,本地主机上的一切都很好。
另外...我没有使用相对路径,我使用的是 AppAsset,它看起来像这样:
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}