apache - 使用 Apache2 重定向的 LetsEncrypt

标签 apache ssl apache2 lets-encrypt certbot

我正在为我的域的 https 重定向而苦苦挣扎。我的域由 godaddy 托管,我的服务器由 ionos 托管。我能够安装 ssl 认证,如果我直接访问 https://domain.com,它会按预期工作。不幸的是,重定向根本不起作用。

conf 文件看起来像这样:

<VirtualHost *:80>
ServerAdmin admin@domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

和这样的 ssl conf 文件:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin@domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.

# RewriteCond %{SERVER_NAME} =domain.com [OR]
# RewriteCond %{SERVER_NAME} =www.domain.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]


Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>

我希望有人对此有想法。

非常感谢!

最佳答案

这应该有效,将它放在虚拟主机之外(可能没有什么不同),并确保 mod_rewrite 已打开。

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

编辑:
为重定向添加了标志 R=301 和 L。

信息:
如果当你把它放在虚拟主机之外时上面的方法不起作用,你应该在 RewriteEngine On 之后添加以下内容:

RewriteOptions InheritDown

关于apache - 使用 Apache2 重定向的 LetsEncrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654240/

相关文章:

apache2 - Apache 2.2 反向代理不工作

django - 使用 mod_wsgi for Django 配置 Apache 2 - 没有 httpd.conf 文件

c - mod_auth_token 安装问题

c++ - 如何在 Apache 中托管 C 程序?

Apache 正在将文件夹重定向到同名的 .html 文件

ssl - letsencrypt似乎正在将域名重定向到81.21.76.62

ssl - 在 Heroku 上使用自定义域设置 SSL 端点

ssl - 跳过 IIB 中的证书验证

php - 在命令行 ubuntu 16.04 上切换 php 版本

apache - Ubuntu 16.04 上的数学