2015-05-18 18:41:49 9014次浏览 3条回答 0 悬赏 3 金钱

我在config/web.php 页面中添加了代码

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,//隐藏index.php 
    // 'enableStrictParsing' => false,
    // 'suffix' => '.html',//后缀,如果设置了此项,那么浏览器地址栏就必须带上.html后缀,否则会报404错误
    'rules' => [
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ],
],

我访问的URL是 http://localhost/yii2test/web/index/index,发现报错,http://localhost/yii2test/web/?r=index/index 这种可以,但我想用上面那种,求大神,我用的是 nginx,是不是要配置这个,怎么配置?

  • 回答于 2015-05-18 20:02 举报

    请楼主使用markdown格式发帖,已经帮你改过了,以后再不按规定发帖,可能会被删帖。

  • 回答于 2015-05-18 20:04 举报

    如果使用的是apache,需要修改.htaccess为如下内容:

    IndexIgnore */*
    RewriteEngine on
    
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php
    
    1 条回复
    回复于 2015-08-22 17:52 回复

    您好,这个文件放在哪里呢?

  • 回答于 2015-05-18 21:59 举报

    这个很简单,你按着我的配置来即可。

    server {
            charset utf-8;
            client_max_body_size 128M;
            listen          80;
            server_name     local.yii2test.com
            index           index.php index.html index.htm;
            root            /usr/local/nginx/html/yii2test/web;
    
            location ~ \.php$ {
                root           /usr/local/nginx/html/yii2test/web;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                include        fastcgi_params;
            }
            #yii框架使用代码
            location / {
                # Redirect everything that isn't a real file to index.php
                try_files $uri $uri/ /index.php?$args;
            }
    
            location ~ /\.ht {
                    deny  all;
            }
        }
    

    上面我给你做了一个虚拟域名,你按着我的配置就可以正常访问了。 url 最终是这样的 local.yii2test.com/index

    4 条回复
    回复于 2015-05-19 10:17 回复

    改了也没用啊,只要我把 ?r= 这个一去掉,就报404

    回复于 2015-12-29 16:37 回复

    大神,我用的apache,怎么能把url中的web路径弄掉呢,我用apache重写弄掉了,但是使用Url::to()方法产生的url还是带有web。。。

    回复于 2016-01-01 16:17 回复

    检查一下 ①web/,htaccess 是否存在 ② urlManager 是否配置 ③ apache rewrite 模块开启了吗

    回复于 2016-01-01 20:22 回复

    我把虚拟主机配置到web目录了,把文件目录指向上一层就好了。

您需要登录后才可以回答。登录 | 立即注册
huanglong
副总裁

huanglong

注册时间:2015-05-11
最后登录:2016-09-07
在线时长:17小时39分
  • 粉丝4
  • 金钱4996
  • 威望0
  • 积分5166

热门问题