PHP中使用ht-access的重定向问题过多

时间:2019-05-13 11:06:15

标签: php .htaccess

如果URL在脚本中没有http或https,我将尝试重定向用户。我的代码在https上运行良好,但是当我尝试使用脚本http时,它会给我太多重定向。我的代码有什么问题?

if (!(isset($_SERVER['HTTPS']) 
    && ($_SERVER['HTTPS'] == 'on' 
    || $_SERVER['HTTPS'] == 1) 
    ||  isset($_SERVER['HTTP_X_FORWARDED_PROTO']) 
    && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) {
         $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
         header('HTTP/1.1 301 Moved Permanently');
         header('Location: ' . $redirect);
         exit();
}

我还在htaccess中使用以下https重定向代码:

# HTTPS redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]
</IfModule>

如果用户未从url写入http或https,我将尝试重定向。就像用户写webbadress.com一样,代码应重定向到用户http://www.webbadress.com/https://www.webbadress.com/

如果包含SSL证书,则php代码可以正常工作。如果没有包含SSL证书,反正我的代码可以工作吗?

0 个答案:

没有答案