【发布时间】:2025-12-07 00:05:02
【问题描述】:
我很难在 Yii 项目中配置我的 .htaccess 和 urlManager 以使前端位于 http://www.example.com 中,后端位于 http://www.example.com/backend 中,并具有以下文件夹结构。欢迎任何帮助。谢谢。
/assets
/backend
/controllers
/config
main.php
/models
/views
/common
/models
/protected
/controllers
/config
main.php
/models
/views
.htaccess
backend.php
index.php
解决方案:在@bool.dev 的大力帮助下一切正常,所以我在这里添加了所有需要的最终文件。在前端,我使用 url 的路径格式并隐藏 index.php
/backend/config/main.php
$backend=dirname(dirname(__FILE__));
Yii::setPathOfAlias('backend', $backend);
return array(
'basePath' => $backend,
'controllerPath' => $backend.'/controllers',
'viewPath' => $backend.'/views',
'runtimePath' => $backend.'/runtime',
...);
/protected/config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
.htaccess
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yii/example/
RewriteRule backend backend\.php [T=application/x-httpd-php]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
后端.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/backend/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
index.php
$yii=dirname(__FILE__).'/../../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii);
Yii::setPathOfAlias('common', dirname(__FILE__).DIRECTORY_SEPARATOR.'common');
Yii::createWebApplication($config)->run();
【问题讨论】:
-
你能告诉我你为什么不把后端做成一个模块吗?
-
我想按照Qiang所描述的Yii项目站点yiiframework.com/wiki/155/…的目录结构。
-
你不应该有一个单独的 htaccess 用于后端,在后端文件夹内吗?比如说为 example.com 创建外部 htaccess,并在后端创建另一个 htaccess,例如.com/backend跨度>
-
我最后的评论可能无关紧要。所以你主要关心的是如何将 example.com/backend 映射/路由到文件夹'backend'?
-
是的,没错,我有两个入口脚本:
index.php用于前端,backend.php用于后端。在文件夹后端和受保护的内部,我有.htaccess和deny from all。
标签: .htaccess yii frontend backend