如何在Yii Framework中删除index.php和?r =

时间:2014-03-05 07:45:53

标签: yii

How to remove "index.php" and also wants to remove "?r=". I want to make URLs user friendly.

如何删除“index.php”并且还要删除“?r =”。我想让网址用户友好。

2 个答案:

答案 0 :(得分:2)

执行以下3个步骤:

  1. 在Apache上重新编写Url。

  2. 将.htaccess放在项目的根目录

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    
  3. 在你的配置中,protected / config / main.php将showScriptName设置为false,这样就是你的网址管理器组件>> urlManager

    'urlManager'=>array(
      'urlFormat'=>'path',
      'rules'=>array(
          '<controller:\w+>/<id:\d+>'=>'<controller>/view',
          '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
          '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
      ),
      'showScriptName'=>false,
    )
    

答案 1 :(得分:1)

1)通过将以下行添加到数组中,启用受保护的&gt;&gt; config&gt;&gt; main.php中的urlManager:

    'urlManager'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
  'showScriptName'=>false,
    ),

2).htaccess文件应该在index.php级别的站点根目录中,并且应该是这样的:

    RewriteEngine on

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php)

    # otherwise forward it to index.php
    RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]

3)确保您的Apache配置 AllowOverride All