【问题标题】:Yii: .htaccess and urlManager for separate backend and frontendYii:.htaccess 和 urlManager 用于单独的后端和前端
【发布时间】: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 用于后端。在文件夹后端和受保护的内部,我有 .htaccessdeny from all

标签: .htaccess yii frontend backend


【解决方案1】:

根据this wiki article by Qiang,您可以进行以下更改,它应该可以工作:

// backend.php:
require('path/to/yii.php');
Yii::createWebApplication('backend/config/main.php')->run();

然后在您的后端配置中(即 backend/config/main.php):

$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);
Yii::setPathOfAlias('backend', $backend);

return array(
    'basePath' => $backend,

    'controllerPath' => $backend.'/controllers',
    'viewPath' => $backend.'/views',
    'runtimePath' => $backend.'/runtime',

    'import' => array(
        'backend.models.*',
    ),
    // ... other configurations ...
);

但要使其工作,我们需要主 .htaccess 将 example.com/backend 路由到 backend.php,我还没有弄清楚。

编辑:
刚刚想通:

RewriteEngine On
RewriteBase /projectroot/
RewriteRule backend backend\.php [T=application/x-httpd-php]

RewriteBase 对我来说很重要,因为当我没有给出正确的 projectroot 时找不到 backend.php,基本上它应该是目录你有入口脚本 backend.php.

【讨论】:

  • 到目前为止还没有,该指南是我尝试遵循的第一个指南,但没有办法让它发挥作用......
  • 实际上我尝试了您的目录结构,并且它有效,所以您能告诉我确切的错误吗?或您遇到的问题?
  • .htaccess 中只有那三行吗?
  • 是的,但是根据您看到的错误,您可以尝试添加更多类似Options +Indexes +FollowSymLinks 的内容,如果您告诉我错误,那么我们可以尝试一下