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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?