在我的 Yii2 项目中,我将 URL 从 myproject/backend/web 重写为 myproject/admin,将 URL 从 myproject/frontend/web 重写为 myproject/。前端工作正常,但在后端,common/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 occurredRewriteRule ^(frontend|backend)/web/ - [L]# Handle the case of backend, skip ([S=1]) the following rule, if current matchedRewriteRule ^admin(/(.*))?$ backend/web/$2 [S=1]# handle the case of frontendRewriteRule .* 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', ],我在 frontend/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 都重定向到前端。
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
我的 .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.php
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报
0/150
提交
取消