gpt4 book ai didi

php - 使用 .htaccess 和 mod_rewrite 强制 SSL/https

转载 作者:IT老高 更新时间:2023-10-28 11:39:12 29 4
gpt4 key购买 nike

如何使用 PHP 中特定的 .htaccess 和 mod_rewrite 页面强制使用 SSL/https。

最佳答案

对于 Apache,您可以使用 mod_ssl使用 SSLRequireSSL Directive 强制 SSL :

This directive forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for the current connection. This is very handy inside the SSL-enabled virtual host or directories for defending against configuration errors that expose stuff that should be protected. When this directive is present all requests are denied which are not using SSL.

虽然这不会重定向到 https。要重定向,请使用 mod_rewrite 尝试以下操作在你的 .htaccess 文件中

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

或在

中给出的各种方法中的任何一种

如果您的提供商禁用了 .htaccess,您也可以在 PHP 中解决此问题(这不太可能,因为您要求它,但无论如何)

if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
header("Status: 301 Moved Permanently");
header(sprintf(
'Location: https://%s%s',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
));
exit();
}
}

关于php - 使用 .htaccess 和 mod_rewrite 强制 SSL/https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398951/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com