PHP重定向后获取服务器协议

时间:2016-12-08 20:18:52

标签: php .htaccess

SOF上有很多类似的主题,但它们似乎没有回答我所面临的具体问题。我使用以下函数来识别服务器协议(http / https)。

public static function srvProto() {
    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
    {
        return 'https';
    }
    return 'http';
}

效果很好。问题是如果我的原始请求附带http,则上述函数返回http。但是,我在.htaccess中重写了一个网址,将所有http次请求重定向到https

RewriteRule ^(.*)$ https://www.mydomain/$1 [R,L]

在网址重写后,我想确定新网址的协议而不是原始协议。我该怎么做呢 ?

更新

我的$ _SERVER的vardump:

[PATH] => /usr/local/bin:/usr/bin:/bin 
[TEMP] => /tmp 
[TMP] => /tmp 
[TMPDIR] => /tmp 
[HTTP_ACCEPT] =>     text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
[HTTP_ACCEPT_ENCODING] => gzip, deflate, sdch 
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8 
[HTTP_CONNECTION] => keep-alive 
[CONTENT_LENGTH] => 0 
[HTTP_COOKIE] => xxx
[HTTP_HOST] => mydomain.ca 
[HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36 
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1 
[REDIRECT_UNIQUE_ID] => WEnRak32qKIAB-L565AAAABG 
[REDIRECT_STATUS] => 200 
[UNIQUE_ID] => WEnRak32qKIAB-L565AAAABG 
[SERVER_SIGNATURE] => 
[SERVER_SOFTWARE] => Apache 
[SERVER_NAME] => mydomain.ca 
[SERVER_ADDR] => 77.0.0.0
[SERVER_PORT] => 80 
[REMOTE_ADDR] => 206.223.191.106 
[DOCUMENT_ROOT] => /home/mydomain/public_html 
[REQUEST_SCHEME] => http 
[CONTEXT_PREFIX] => 
[CONTEXT_DOCUMENT_ROOT] => /home/mydomain/public_html 
[SERVER_ADMIN] => webmaster@mydomain.ca 
[SCRIPT_FILENAME] => /home/mydomain/public_html/admin/default.php 
[REMOTE_PORT] => 53936 
[REDIRECT_URL] => /admin/dashboard 
[REDIRECT_QUERY_STRING] => controller=dashboard 
[SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => GET 
[QUERY_STRING] => controller=dashboard 
[REQUEST_URI] => /admin/dashboard 
[SCRIPT_NAME] => /admin/default.php 
[PHP_SELF] => /admin/default.php 
[REQUEST_TIME_FLOAT] => 1481232746.157 
[REQUEST_TIME] => 1481232746

0 个答案:

没有答案