2014-09-26 52 views
1

我有这个htaccess文件:如何在apache/.htaccess/mod_rewrite中隐藏yii的index.php文件?

RewriteEngine on 
#if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php 

而且我使用Yii 1.1.15(这个网站是从1.1.13采取版本,但是这样的版本不再提供下载,所以我在1.1.15框架使用它安装 - althought却没有这个问题,据我所看到的),有喜欢的conf/main.php文件:

<?php 

return array(
    'basePath'=>dirname(dirname(__FILE__)), 
    'name'=>'Grand Duval - Códigos premiados', 
    'defaultController'=>'participantes/create', 
    'language'=>'es', 

    // preloading 'log' component 
    'preload'=>array('log'), 

    // autoloading model and component classes 
    'import'=>array(
     'application.models.*', 
     'application.components.*', 
    ), 

    'modules'=>array(
     'gii'=>array(
      'class'=>'system.gii.GiiModule', 
      'password'=>'mypassword', 
      'ipFilters'=>array('127.0.0.1', '::1'), 
     ), 
    ), 

    // application components 
    'components'=>array(
     'user'=>array(
      // enable cookie-based authentication 
      'allowAutoLogin'=>true, 
     ), 

     '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>', 
      ), 
     ), 

     'db'=>array(
      'connectionString' => 'mysql:host=localhost;dbname=mydatabase', 
      'emulatePrepare' => true, 
      'username' => 'sbuser', 
      'password' => 'dbpassword', 
      'charset' => 'utf8', 
     ), 

     'errorHandler'=>array(
      'errorAction'=>'site/error', 
     ), 
     'log'=>array(
      'class'=>'CLogRouter', 
      'routes'=>array(
       array(
        'class'=>'CFileLogRoute', 
        'levels'=>'error, warning', 
       ), 
      ), 
     ), 
    ), 

    // application-level parameters that can be accessed 
    // using Yii::app()->params['paramName'] 
    'params'=>array(
     // this is used in contact page 
     'adminEmail'=>'[email protected]', 
    ), 
); 

的重要组成部分,这里是:

 '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>', 
      ), 
     ), 

这样的配置旨在 - 并且确实如此 - 为控制器controller和动作action生成绝对url,如http://www.mydomain.dev/controller/action

我与.htaccess文件有问题:如果我尝试点击http://www.mydomain.dev/controller/action,则找不到网址。但是,如果我点击http://www.mydomain.dev/index.php/controller/action它被发现。看起来.htacccess根本没有任何效果(如果我删除.htaccess文件,则完全相同)。

问题:我做错了什么?

回答

1

我找到了答案。 确保mod_rewrite对我的其他网站有效我问自己:我的.htaccess是否被识别?

,所以我想通过我的破坏.htaccess文件来强制500错误:

RewriteEngine on 
# if a directory or a file exists, use it directly 
TryToForceA500 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php 

,并不会触发错误!所以我问自己 - 海因 - 为什么不被承认?和检查了网站.conf文件,结果发现:

AllowOverride none 

而改为:

AllowOverride all 

(这只是一个开发服务器,这样设置是为我好)

然后文件被识别和工作!

1

我认为你需要启用mod_rewrite的尝试:

a2enmod rewrite 

然后,只需重新启动服务器。

+0

不,它已启用。否则,我会得到一个500错误。顺便说一句,我现在试过这个命令,但都没有工作 – 2014-09-26 14:31:10

+0

虽然你写的答案不是我的问题的答案(我发现它),但这是人们发现的一个常见问题,而且我前一段时间发现了这个问题。在Apache中确保mod_rewrite是活动的并不是微不足道的(尽管它在nginx中的'表哥'是微不足道的),而且大部分时间都是解决方案。 upvote :)。 – 2014-09-26 14:38:36