thinkphp 重定向 url pathinfo 这里有个坑

摘要:url编码,thinkphp重定向,http GET请求。
环境:thinkphp,windows wamp
一、这里有个坑
配置项:
URL模式为:pathinfo

class IndexController extends Controller {
    public function index(){
        $params = array(
            'q' => 'q',
            'w' => 'w',
            'u' => 'http://www.baidu.com'
        );
        $this->redirect('test',$params);

    }
    public function test(){
        dump(I('get.'));
    }
}

  如果访问index方法,会报错:
Not Found

The requested URL /tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http://www.baidu.com.html was not found on this server.

Apache/2.4.23 (Win64) PHP/5.6.25 Server at localhost Port 80
  这是为什么呢,下来就来讨论这个。

二、http请求
  有没有人特意对比过这样几个url的区别呢?

http GET header
http GET body
parse_url()
$_SERVER[‘REQUEST_URI’]
$_SERVER[‘QUERY_STRING’]
$_GET

三、thinkphp 页面跳转方法的处理过程

$this->redirect('test',$params);

  U()函数生成要跳转的url为:
/tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http%3A%2F%2Fwww.baidu.com.html
再302重定向,查看apache 的access.log可以看到请求的URL是:
“GET /tpProject/tpTest/index.php/Home/Index/test/q/q/w/w/u/http%3A%2F%2Fwww.baidu.com.html HTTP/1.1” 404 361
apache 找不到目标文件

  所以,如果GET参数里面有url的话,及时是经过url编码过的,如果是pathinfo 模式的url还是会有问题,保险起见的方式是:

public function index(){
    $params = array(
        'q' => 'q',
        'w' => 'w',
        'u' => 'http://www.baidu.com'
    );
    $url = U('test') . '?' . http_build_query($params);
    header('location:' . $url);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值