![Apache 重定向循环](https://linux22.com/image/666930/Apache%20%E9%87%8D%E5%AE%9A%E5%90%91%E5%BE%AA%E7%8E%AF.png)
有人能解释一下为什么我总是遇到重定向循环吗?我正尝试将 https 设为默认端口。我在 ec-2 实例上运行 Apache:
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName myserver.com
RewriteEngine on
RewriteRule ^/$ https://myserver.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/clients"
ServerName clients.myserver.com
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
ServerName www.myserver.com
SSLCertificateFile /etc/ssl/certs/myserver.com.crt
SSLCertificateKeyFile /etc/ssl/certs/private-key-decrypted.key
</VirtualHost>
答案1
尝试“重定向”而不是“重写” 这是我的配置作为参考。不过说实话,我并不完全确定你为什么会遇到这个问题。只能说这个配置似乎有效。
Listen 80
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerAdmin root@localhost
DocumentRoot "/var/www/html"
ServerName <server>.<domain>.<extension>
ServerAlias <server>
Redirect permanent / https://<server>.<domain>.<extension>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin root@localhost
DocumentRoot "/var/www/html"
ServerName <server>.<domain>.<extension>
ServerAlias <server>
SSLEngine on
SSLCertificateFile /etc/certs/<server>.crt
SSLCertificateKeyFile /etc/certs/<server>.key
</VirtualHost>
答案2
尽管mod_rewrite
可以使用,但不建议使用,因为它可能会导致重定向循环,如您的示例所示。使用Redirect
是一种更好的方法。您的配置可能如下所示:
<VirtualHost *:80>
ServerName yourserver.com
Redirect permanent / https://yourserver.com/
</VirtualHost>
<VirtualHost *:443>
ServerName yourserver.com
DocumentRoot "/var/www/html"
SSLEngine On
...
</VirtualHost>
这也意味着每个主机需要有两个VirtualHost
部分。以下是文档的链接https://wiki.apache.org/httpd/RedirectSSL。