问题
I try to use Yiinitializr site structure for my Yii project. The structure looks like this:
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
The question is how to make working URLs like this:
admin.mysite.com/invites - in backend
and
mysite.com/users - in frontend
Troubles with routing appeared when i uploaded project on virtual hosting. Backend for example:
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 folder has own .htaccess with this content:
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 have rewrite rules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
And Yii urlManagers code:
'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:
UPDATED AFTER discussion in the chat:
All problem will be in $_SERVER['SCRIPT_NAME']
, it equals /backend/www/index.php
. That's why parsing of request in CHttpRequest::getPathInfo
is empty value, because $baseUrl (it's a basedir of $_SERVER['SCRIPT_NAME'] not ''
) and $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']
.
Two solutions:
- Set on the beginning of index.php
$_SERVER['SCRIPT_NAME'] = '/index.php';
- Make that subdomain
admin.example.com
see to the directory/backend/www/
directly.
来源:https://stackoverflow.com/questions/19676821/how-to-create-friendly-links-in-yii-project-using-yiinitializr