当前位置: 动力学知识库 > 问答 > 编程问答 >

php - HTTPS only on specific pages - Htaccess, Apache, OpenSSL

问题描述:

I'm trying to get HTTPS to work as I want, but with no luck.

What I like to do is that I want the website to go over http, and https on specific url's. The specific url's is: reg.php & glomtpass.php.

But when I try the code, it says: "This webpage has a redirect loop". After a bunch of attempts it still have that error.

The HTTP -> HTTPS code I use in .htacess right now:

RewriteCond %{HTTPS} on

RewriteCond %{SCRIPT_FILENAME} !\/(reg|glomtpass)\.php [NC]

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Any help is appreciated!

网友答案:

If it is not too much trouble to add a functions file to the top of your pages then this is how I check move in and out of HTTPS in php. To use it just require the file you put this in on all your scripts.

functions file

<?php

$scriptsWithSSL = array('reg.php','glomtpass.php');
if (in_array(end(explode('/', $_SERVER['PHP_SELF'])), $scriptsWithSSL) === true) {
    //we are on a page we want to be HTTPS. will become HTTPS if requested with HTTP
    if ($_SERVER['HTTPS'] == '' || $_SERVER['HTTPS'] == 'off') {
        header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']);
        exit;
    }
} else {
    //page should not be HTTPS and will move to HTTP if requested with HTTPS
    if (strtolower($_SERVER['HTTPS']) == 'on') {
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERY_STRING']);
        exit;
    }
}
?>

What this will do is any script that is found inside the $scriptsWithSSL array with be moved to HTTPS if it is not requested with HTTPS. Otherwise its a normal page and will be moved from HTTPS to HTTP if it was not requested with HTTP.

分享给朋友:
您可能感兴趣的文章:
随机阅读: