我正在尝试让 HTTPS 正常工作,但没有成功。 我喜欢做的是我希望网站通过 http 和特定 url 上的 https。具体的 url 是:reg.php & glomtpass.php。
但是当我尝试代码时,它说:“这个网页有一个重定向循环”。经过多次尝试,它仍然有这个错误。
我现在在 .htacess 中使用的 HTTP -> HTTPS 代码:
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/(reg|glomtpass)\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
感谢任何帮助!
最佳答案
如果在页面顶部添加一个函数文件不是太麻烦,那么这就是我在 php 中检查进出 HTTPS 的方法。要使用它,只需要将它放入所有脚本中的文件即可。
函数文件
<?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;
}
}
?>
这将做的是在 $scriptsWithSSL
数组中找到的任何脚本,如果没有使用 HTTPS 请求,则将其移动到 HTTPS。否则它是一个普通页面,如果不是用 HTTP 请求的,它将从 HTTPS 移动到 HTTP。
关于php - 仅在特定页面上使用 HTTPS - Htaccess、Apache、OpenSSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574387/