douhan5853 2014-10-01 18:41
浏览 32
已采纳

punycode和.рф西里尔域重定向

I have a website with cyrillic domain name. There is an authorization lib which redirects the user to login page, but the url is somehow missformed.

The website is on CodeIgniter and the redirect function used is the standard redirect function of the codeigniter. I have modified a bit and it looks now like this

function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
    if ( ! preg_match('#^https?://#ui', $uri))
    {
        $uri = site_url($uri);
    }


    //exit(idn_to_ascii($uri));
    switch($method)
    {
        case 'refresh'  : header("Refresh:0;url=http://".idn_to_ascii($uri));
            break;
        default         : header("Location:http://".idn_to_ascii($uri), TRUE, $http_response_code);
            break;
    }
    exit;
}

idn_to_ascii functions seems not rightly coding the UTF url string ...

Can anybody hint a solution?

Instead of

http://xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--p1ai/auth/admin/

I get

xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--/auth/login-foj4c

Any ideas ?

  • 写回答

1条回答 默认 最新

  • duanjiong5023 2014-10-01 18:46
    关注

    You should change your code a bit and use idn_to_ascii() only on domain part and not on the full url.

    instead of:

    switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=http://".idn_to_ascii($uri));
                break;
            default         : header("Location:http://".idn_to_ascii($uri), TRUE, $http_response_code);
                break;
        }
    

    you should use:

    $pos = mb_strpos($uri,'/', null, 'UTF8');
    
    if ($pos === false) { // only domain, no slash here
        $uri = idn_to_ascii($uri);
    }
    else { // changes only for domain part, rest left unchanged
        $uri = idn_to_ascii(mb_substr($uri,0,$pos, 'UTF-8')).mb_substr($uri,$pos,null, 'UTF-8');
    }
    
    
    switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=http://".$uri);
                break;
            default         : header("Location:http://".$uri, TRUE, $http_response_code);
                break;
        }
    

    EDIT

    Test code just for generating URL:

    <?php
    
    $uri = 'помощь-от-сглаза-и-порчи.рф/auth/admin/';
    
    $pos = mb_strpos($uri,'/', null, 'UTF8');
    
    if ($pos === false) {
        $uri = idn_to_ascii($uri);
    }
    else {
        $uri = idn_to_ascii(mb_substr($uri,0,$pos, 'UTF-8')).mb_substr($uri,$pos,null, 'UTF-8');
    }
    
    echo $uri."<br />";
    

    Output for this is: xn-------63dat7alb0aizbbjcoujt7j3a6e.xn--p1ai/auth/admin/ as expected I think - when I copy this text into browser I get redirected again to http://помощь-от-сглаза-и-порчи.рф/auth/admin/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目