How to create friendly links in Yii project using Yiinitializr

纵然是瞬间 提交于 2020-01-05 02:30:14

问题


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:

  1. Set on the beginning of index.php $_SERVER['SCRIPT_NAME'] = '/index.php';
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!