在我的Yii2项目中,我将URL从myproject/backend/web重写为myproject/admin,并将其从myproject/前端/web重写为myproject/。
前端运行良好,但在后端,公共/web/css/和common/web/js/没有加载,我将所有这些重定向到前端,并给出404页面未找到错误。
我检查了css和js的路径,如下所示是正确的: myproject/common/web/css/style.css。所有css和js都加载了正确的路径,但它会重定向到其他位置。
我已经使用了以下代码来重写:
我添加了这个.htaccess:
RewriteEngine On
# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]
# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^admin(/(.*))?$ backend/web/$2 [S=1]
# handle the case of frontend
RewriteRule .* frontend/web/$0
# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php我在backend/config/main.php中添加了这两行:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'scriptUrl'=> ($_SERVER['HTTP_HOST'] == "localhost") ? '/chiefsRS/admin' : '/admin',
'rules' => [
'login'=>'site/login',
'dashboard'=>'site/index',
]
],
'request' => [
'baseUrl'=>($_SERVER['HTTP_HOST'] == "localhost") ? '/myproject/admin' : '/admin',
],我在前端/config/main.php中添加了这两行:
'request' => [
'baseUrl'=> ($_SERVER['HTTP_HOST'] == "localhost") ? '/myproject' : '',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'scriptUrl'=>($_SERVER['HTTP_HOST'] == "localhost") ? '/myproject' : '',
'rules' => [
'home'=>'site/index',
]
]在我的本地服务器上,一些来自普通的css被加载了,还有一些给出了404错误。在实时服务器中,所有来自common/web的css和js都会给我404页找不到错误。错误页面有前端的导航栏,所以我认为所有的css和js都重定向到了前端。
发布于 2019-06-20 19:17:31
我的.htaccess文件有问题。我更改了htaccess,如下所示:
RewriteEngine On
RewriteRule ^(common)/ - [L]
# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]
# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^admin(/(.*))?$ backend/web/$2 [S=1]
# handle the case of frontend
RewriteRule .* frontend/web/$0
# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.phphttps://stackoverflow.com/questions/56651540
复制相似问题