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