简体   繁体   English

仅在特定页面上使用HTTPS-Htaccess,Apache,OpenSSL

[英]HTTPS only on specific pages - Htaccess, Apache, OpenSSL

I'm trying to get HTTPS to work as I want, but with no luck. 我正在尝试使HTTPS能够按我的意愿工作,但是没有运气。 What I like to do is that I want the website to go over http, and https on specific url's. 我想做的是让网站浏览http和特定网址上的https。 The specific url's is: reg.php & glomtpass.php. 具体的网址是: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: 我现在在.htacess中使用的HTTP-> HTTPS代码:

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. 如果在页面顶部添加功能文件不是很麻烦,那么这就是我检查在php中移入和移出HTTPS的方式。 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. 这将是在$scriptsWithSSL数组中找到的任何脚本,如果未通过HTTPS请求将其移至HTTPS。 Otherwise its a normal page and will be moved from HTTPS to HTTP if it was not requested with HTTP. 否则,其为正常页面,如果未通过HTTP请求,则将从HTTPS移至HTTP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM