AWS中的虚拟主机HTTPS重定向

时间:2018-04-13 14:06:57

标签: apache amazon-web-services ssl https virtualhost

我有两个域名abc.example.com和xyz.example.com。我从AWS获得了* .example.com的SSL证书(我无法下载SSL证书)。使用虚拟主机文件将两个域指向两个不同的文件夹/ var / www / html / abc和/ var / www / html等/ XYZ。现在我想对虚拟主机中的两个域使用从http到https的重定向。我试过这样的虚拟主机代码:

< VirtualHost *:80 >
    ServerAdmin root@localhost
    DocumentRoot "/var/www/html/abc"
    ServerName abc.example.com
    Redirect permanent / https://abc.example.com/
    ServerAlias www.abc.example.com
< /VirtualHost >   
< VirtualHost *:80 >
    ServerAdmin root@localhost
    DocumentRoot "/var/www/html/xyz"
    ServerName xyz.example.com
    Redirect permanent / https://xyz.example.com/
    ServerAlias www.xyz.example.com
< /VirtualHost >

并尝试*:443而不是*:80。但我无法得到任何结果。 请帮我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

花费大量时间后,终于找到了解决问题的办法。 代码以这种方式工作:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin root@localhost
    VirtualDocumentRoot "/var/www/html/abc"
    ServerName abc.example.com
    <Directory "/var/www/html/abc">
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
    DirectoryIndex index.html index.php   
    </Directory>   
</VirtualHost>

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin root@localhost
    VirtualDocumentRoot "/var/www/html/xyz"
    ServerName xyz.example.com
    <Directory "/var/www/html/xyz">
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    RewriteCond %{REQUEST_URI} /+[^\.]+$
    RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
    DirectoryIndex index.html index.php   
    </Directory>   
</VirtualHost>