如何使用Yiinitializr在Yii项目中创建友好链接

时间:2013-10-30 08:04:02

标签: php apache .htaccess yii

我尝试将Yiinitializr网站结构用于我的Yii项目。结构如下所示:

root
--backend
----standart yii folders
----...
----www
------index.php (admin.mysite.com)
--common
----common folders for backend and frontend
----...
--backend
----standart yii folders
----...
----www
------index.php (mysite.com)

Blank Yiinitializr strucure see on github

问题是如何制作这样的工作网址:

admin.mysite.com/invites - in backend

mysite.com/users - in frontend

当我在虚拟主机上传项目时出现了路由问题。后端例如:

admin.mysite.com/backend/www/?r=site/invites - working properly
admin.mysite.com/backend/www/invites - working properly
admin.mysite.com/?r=site/invites - working properly
admin.mysite.com/invites - redirects to index

Root文件夹中包含以下内容的.htaccess:

RewriteEngine On 
RewriteRule ^backend/www/ - [last] 
RewriteCond %{HTTP_HOST} admin.mysite.com [nocase] 
RewriteRule (.*) backend/www/$1 [last]

RewriteRule ^frontend/www/ - [last] 
RewriteCond %{HTTP_HOST} !admin.mysite.com [nocase] 
RewriteRule (.*) frontend/www/$1 [last]

backend / www / .htaccess有重写规则:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
</IfModule>

和Yii urlManagers代码:

'urlManager' => array(
    // uncomment the following if you have enabled Apache's Rewrite module.
    'urlFormat' => 'path',
    'showScriptName' => false,

    'rules' => array(
        // default rules
        '<page:\w+>' => 'site/<page>',
        '' => 'site/index',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
),

1 个答案:

答案 0 :(得分:1)

聊天后更新讨论:
所有问题都在$_SERVER['SCRIPT_NAME'],等于/backend/www/index.php。这就是为什么在CHttpRequest::getPathInfo中解析请求是空值的原因,因为$ baseUrl(它是$ _SERVER ['SCRIPT_NAME']的基础而不是'')和$_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']

两种解决方案:

  1. 设置在index.php $_SERVER['SCRIPT_NAME'] = '/index.php';
  2. 的开头
  3. 让该子域admin.example.com直接查看目录/backend/www/