我被这个困住了...
我有一个 Yii 项目(Yii 1.1.14),它在我的本地机器上运行良好(IIS 7.5 with ISAPI rewrite 3, Php 5.5)。
然而,在测试服务器(Linux、Apache 2.2、Php 5.5)上,除了重定向之外,一切正常。我在主页上得到一个无限重定向循环,因为登录和索引控制器正在相互重定向(这根本不应该发生)。
如果我将登录操作放在 url (www.mysite.com/site/login) 中,我可以登录。但是,如果我在任何控制器操作中进行重定向(例如,在更新一些数据之后),我会被抛出到主 url,而不是我重定向到的路由。如果我使用 .htaccess 重写规则没有区别,所以我想它一定是 Yii 中的一些配置和一些特定于 apache 的配置内容......
这是我的 urlManager 配置:
'homeUrl'=>array('/site/index'),
'components'=>array(
'user'=>array(
'loginUrl'=>array('site/login'),
// disable cookie-based authentication
'allowAutoLogin'=>false,
'class'=>'MyWebUser'
),
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
),
),
)
这是我的 .htaccess 文件:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# 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
RewriteRule ^(.*)\?*$ index.php?$1 [L,QSA]
还有我的站点/登录和站点/索引操作导致无限循环(它们在我的本地机器上按预期工作):
public function actionIndex()
{
if(Yii::app()->user->isGuest) {
$this->redirect('/site/login');
} else {
$this->render('index');
}
}
public function actionLogin()
{
// redirect to index page if user is already logged in
if(!Yii::app()->user->isGuest) {
$this->redirect(Yii::app()->homeUrl);
}
$model = new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm'])) {
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login()) {
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('login',array('model'=>$model));
}
直接调用路由有效,重定向无效。
编辑:当我从 切换'urlFormat'=>'path'
到 时'urlFormat'=>'get'
,路由工作。