6

I'm having a hard time to configure my .htaccess and the urlManager in a Yii project to have the frontend in http://www.example.com and the backend in http://www.example.com/backend with the following folder structure. Any help is welcome. Thanks.

/assets
/backend
   /controllers
   /config
      main.php
   /models
   /views
/common
   /models
/protected
   /controllers
   /config
      main.php
   /models
   /views 
.htaccess
backend.php
index.php

Solution: after the great help of @bool.dev everything it's working, so I'm adding here every needed final file. In the frontend I'm using path format for the url and hiding the 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>

backend.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();
6
  • can you tell me why didn't you make backend a module? Commented May 18, 2012 at 8:40
  • I wanted to follow the directory structure of the Yii project site yiiframework.com/wiki/155/… described by Qiang. Commented May 18, 2012 at 8:48
  • shouldn't you have a separate htaccess for backend, inside the backend folder?, say make the outer htaccess for example.com , and create another htaccess inside backend, for example.com/backend Commented May 18, 2012 at 10:36
  • 1
    my last comment might be irrelevant. so your main concern is how to map/route example.com/backend to the folder 'backend'? Commented May 18, 2012 at 10:56
  • Yeah, exactly, I have the two entry scripts: index.php for the frontend and backend.php for the backend. Inside of the folder backend and protected I have .htaccess with deny from all. Commented May 18, 2012 at 11:47

1 Answer 1

6

According to this wiki article by Qiang, you could make the following changes and it should work:

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

Then in your backend's config (i.e 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 ...
);

But for this to work we need the main .htaccess to route example.com/backend to backend.php, which i haven't figured out yet.

Edit:
Just figured out:

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

The RewriteBase was important for me, as the backend.php was not found when i hadn't given the correct projectroot, basically it should be the directory where you have the entry script backend.php.

Sign up to request clarification or add additional context in comments.

11 Comments

Nothing so far, that guide was the first I tryed to follow but there is no way to get it to work...
actually i tried with your directory structure, and it worked, so can you tell me the exact errors? or problems that you are getting?
Do you have just that three lines in the .htaccess?
yes but depending on the errors you see, you could try and add more like Options +Indexes +FollowSymLinks, if you tell me the error, then we could try something
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.