doukuanghuan7582 2018-04-28 21:14
浏览 100
已采纳

如何定义支持HTTPS和HTTP的路径?

I have a redirect character strip script, that takes the original URL, strips the requested strings out of it (foo , bar) and then redirects to the same URL only without these strings.

It's currently set up to work with HTTP Only, as users always requests the HTTP page. But now I'm adding HTTPS, so some users will land on HTTPS. in that case, I'd like the redirect to be to the HTTPS.

How Can I do it?

I've tried simply changing:

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Into:

$url = "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

But it created an invalid request (mydomain.com//mydomain.com....)

CODE:

function unparse_url($parsed_url) {
  $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';
  $pass     = ($user || $pass) ? "$pass@" : '';
  $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  $query    = !empty($parsed_url['query']) ? '?' . trim($parsed_url['query'], '&') : '';
  $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  return "$scheme$user$pass$host$port$path$query$fragment";
}

function strip_query($url, $query_to_strip) {
  $parsed = parse_url($url);
  $parsed['query'] = preg_replace('/(^|&)'.$query_to_strip.'[^&]*/', '', $parsed['query']);
  return unparse_url($parsed);
}

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$url2 = (strip_query($url, 'foo')); # query to strip  - foo
$new_url = (strip_query($url2, 'bar')); # strip also - bar


$filtered = array_filter(array_keys($_GET), function($k) {
    return strpos($k, 'foo') === 0;
});

if ( !empty($filtered) ) {
    $_SESSION['trackParam'] = $_GET; // #### Save original request data and url before redirection
    $_SESSION['REQUEST_URI'] = $_SERVER[REQUEST_URI]; 
    $_SESSION['redirected'] = true;
    header ("Location: $new_url");
}
  • 写回答

2条回答 默认 最新

  • dtt2012 2018-04-28 21:22
    关注

    You can use $_SERVER['HTTPS'] to decide whether to use https in your $url:

    function check_https() {
        return    (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
               || $_SERVER['SERVER_PORT'] == 443;
    }
    
    $url = (check_https() ? "https" : "http")
           ."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应