【问题标题】:How to remove index.php and ?r= in Yii Framework如何在 Yii 框架中删除 index.php 和 ?r=
【发布时间】:2014-03-05 07:45:53
【问题描述】:

如何删除“index.php”也想删除“?r=”。我想让 URL 对用户友好。

【问题讨论】:

标签: yii


【解决方案1】:

执行以下 3 个步骤:

  1. 在 Apache 上启用 Url 重写。

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

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

在您的配置中,protected/config/main.php 将 showScriptName 设置为 false,就像您的 url 管理器组件 >> 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,
)

【讨论】:

  • 如果你遇到了像我一样的问题 - 确保在 appache /etc/httpd/conf/httpd.conf 文件中设置了“AllowOverride All” - 否则你的 .htaccess 文件不会运行上班!
【解决方案2】:

1) 在 protected>>config>>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 不是 None

【讨论】:

    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多