将 http 重定向到 https,端口 80 被其他服务使用

Redirect http to https, port 80 being used by other service

我有 apache (httpd) 为我的网站服务,启用了 SSL(在 https 上,端口 443)。

此外,在同一台服务器上,我有一个单独的进程,它使用端口 80。现在,我想重定向我的网站,如果在纯 http 上请求将其重定向到 https。当然,apache 的配置没有帮助,因为它不在端口 80 上提供服务。请帮助解决这个问题!

您可以使用 .htaccess 重定向

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

根据下面问题的评论回答(无法修改 Apache-conf,独立 PHP 进程)

假设您有以下脚本 staticpageserver.php 运行ning via php -S 0.0.0.0:80 staticpageserver.php (Built-in web server):

<?php
#staticpageserver.php

// Your PHP / static content logic below here
echo 'Some content';

在端口 80 上提供您的内容。对于一个域(例如:your.page.com),您现在想要重定向到端口 443 / HTTPS。 这可以通过检查 Host-header 并在脚本顶部的 PHP 中重定向来实现:

<?php
#staticpageserver.php

if ($_SERVER['HTTP_HOST'] === 'your.page.com') {
    http_response_code(302); // 302 Found Redirect. Maybe use 301 instead.
    header('Location: https://your.page.com' . $_SERVER['REQUEST_URI']);
    exit; // Do not execute further than the redirect
}

// Your PHP / static content logic below here
echo 'Some content';

对于除该域之外的任何其他访问(另一个域,直接通过 IP 地址),您仍然 运行 通过您的脚本。