anyway 2014-10-30 13:30:47 39635次浏览 10条回复 3 2 0

YII2.0 如何去掉URL中的index.php

觉得很赞
  • 回复于 2014-10-30 13:31 举报

    http://www.a.com/index.php/site/index URL 类似地址 如何去掉index.php

    http://www.a.com/site/index 目前去掉也可以正常访问 但是视图生成URL的时候会自动加上index.php

  • 回复于 2014-10-30 16:47 举报

    这个可以通过配置urlManager,enablePrettyUrl showScriotName 这两个参数

  • 回复于 2014-10-31 15:09 举报
    'urlManager' => [             
        'enablePrettyUrl' => true,
        'showScriptName' => true, 
        'rules' => [              
        ],                        
    ],
    

    谢谢
    这样写对么 还是不行

  • 回复于 2014-11-01 00:28 举报
    1. 开启apache-rewrite
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules'=>[
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ],
    ],
    
    1. 在入口文件同级目录下添加.htaccess文件(内容自行百度)
    觉得很赞
  • 回复于 2014-11-01 15:04 举报

    谢谢裤衩的回复
    我的nginx 服务器 我已经开发了 rewrite 并且也添加了.htaccess 但就是不行 yii 1.0 就可以

  • 回复于 2014-11-01 15:06 举报

    问题已解决 showScriptName 参数更改为false 即可

    1 条回复
    回复于 2015-09-01 22:02 回复

    楼主我想问一下你是怎么弄的,我的也是nginx+YII2.0,能加下QQ告知一下么,1009040559

  • 回复于 2015-01-13 23:49 举报

    我用的apache,Yii2的basic模板,但是出现“The requested URL /site/about was not found on this server.”这种错误是为什么?我在apache里配置了虚拟主机

    <VirtualHost *:80>
        DocumentRoot "D:\wamp\www\basic\web"
        DirectoryIndex index.php
        ServerName www.test.com
    </VirtualHost>
    
    <Directory "D:\wamp\www\basic\web">
      Options Indexes FollowSymLinks
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>
    

    index.php同级目录下创建了.htaccess文件,内容是

    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

    我是哪里配错了么?

    2 条回复
    回复于 2016-04-17 22:54 回复

    请问你解决了吗? 我遇到和你同样的问题 我是用advanced模板 也是阿帕奇 虚拟主机

    回复于 2016-07-18 17:30 回复

    你配置好backend或者frontend的config-》main.php了吗,里面需要把components项的urlManager设置enablePrettyUrl=>true,showscriptName=>false,就是不显示index了,最后在脚本入口index.php同级目录下新建一个.htaccess文件,内容如楼上,就可以了

    觉得很赞
  • 回复于 2015-04-19 23:26 举报

    最近在评估YII 2.0,实现perfy url的步骤如下 1 和 2
    1.在 index.php 同级目录建立 .htaccess 文件 ,设置内容:

    RewriteEngine on
    # 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`
    

    2.为 config目录的web.php 增加配置项 urlManager,代码:

    'urlManager' => [		
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
        ),
    ],
    

    以上2个步骤下来,There is Pretty URL ! ~(≧▽≦)/~
    内容引用源:http://www.bsourcecode.com/yiiframework2/removing-index-php-from-url-in-yiiframework-2-0/

    2 条回复
    回复于 2015-08-25 10:29 回复

    按照这个配置还是有问题,不知道为啥

    回复于 2017-06-23 15:49 回复

    index.php前边加个?

  • 回复于 2015-06-16 14:52 举报

    我想问下,是不是我们的URL中的index.php换掉后。加载的JS文件中的图片文件之类的路径就不对了?

  • 回复于 2016-12-08 09:39 举报

    如果是nginx呢?

您需要登录后才可以回复。登录 | 立即注册