【问题标题】:LetsEncrypt with Apache2 RedirectionLetsEncrypt 与 Apache2 重定向
【发布时间】:2020-04-26 11:37:12
【问题描述】:

我正在为我的域的 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>

我希望有人对此有所了解。

非常感谢!

【问题讨论】:

    标签: apache ssl apache2 lets-encrypt certbot


    【解决方案1】:

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

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

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

    【讨论】:

    • 同时删除或注释掉 https 重定向的其他重写
    • 您好,感谢您的回复。我仍然不知道发生了什么,但它现在正在工作。我测试了所有内容,然后决定重新设置所有内容。由于服务器完全是空的,所以很容易做到。现在它正在工作,文件看起来相同。
    • 另外,我已经更改了答案中的最后一行代码,因此请使用新的。请
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2011-05-25
    • 2018-02-10
    • 2021-08-17
    • 2015-05-28
    • 2010-09-28
    相关资源
    最近更新 更多