gpt4 book ai didi

apache - 如何配置规范的主机名并使用 apache 强制使用 https?

转载 作者:行者123 更新时间:2023-12-05 06:45:22 24 4
gpt4 key购买 nike

我想一直使用 https 并拥有一个规范域。问题是当我与辅助域建立 https 连接时,一切都崩溃了,因为证书是针对主域的。有没有办法解决这个问题,还是我必须为我的所有域购买证书,而不仅仅是规范域?

我的“/etc/apache2/apache2.conf”的相关部分

# Canonical hostname and always use https
<IfModule mod_rewrite.c>
RewriteEngine On

# force https on primary host (works fine)
# http://primary-host.com -> https://primary-host.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^primary-host\.com$
RewriteRule (.*) https://primary-host.com%{REQUEST_URI}

# force https and redirect to primary host (works fine)
# http://secondary-host.com -> https://primary-host.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secondary-host\.com$
RewriteRule (.*) https://primary-host.com%{REQUEST_URI}

# FAIL: Certificate doesn't match and "Bad Request (400)" if accepted!
# https://secondary-host.com -> https://primary-host.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^secondary-host\.com$
RewriteRule (.*) https://primary-host.com%{REQUEST_URI}

</IfModule>

最佳答案

您是否按照此处所述尝试过以下操作? https://httpd.apache.org/docs/2.4/rewrite/remapping.html

The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.

Solution: The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

<VirtualHost *:80>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com

Redirect "/" "http://www.example.com/"
</VirtualHost>

<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>

简而言之

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect "/" "http://www.example.com/"
</If>

关于apache - 如何配置规范的主机名并使用 apache 强制使用 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460395/

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