简体   繁体   English

如何在具有别名的虚拟主机上配置 Apache 以将 HTTP 重定向到 HTTPS?

[英]How to configure Apache to redirect HTTP to HTTPS on a virtualhost that have an alias?

I have a virtual site configured as below and it's redirecting all HTTP requests to HTTPS:我有一个如下配置的虚拟站点,它将所有 HTTP 请求重定向到 HTTPS:

<VirtualHost *:80>

        ServerName api.example.com

        ProxyPreserveHost on
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/

        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]

        RewriteCond %{SERVER_NAME} =api.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

But the configuration below is not working, it's just serving the users with the HTTP version of the site:但是下面的配置不起作用,它只是为站点的 HTTP 版本的用户服务:

<VirtualHost *:80>

        ServerName secondexample.co.zw
        ServerAlias www.secondexample.co.zw
        ProxyPreserveHost on
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/

        RewriteEngine on
        RewriteCond %{HTTP:Upgrade} websocket [NC]
        RewriteCond %{HTTP:Connection} upgrade [NC]
        RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]

        RewriteCond %{SERVER_NAME} =www.secondexample.co.zw [OR]
        RewriteCond %{SERVER_NAME} =secondexample.co.zw
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

The only difference I am noticing is that the second configuration has an alias.我注意到的唯一区别是第二个配置有一个别名。 Wha's wrong with the second configuration?第二个配置有什么问题?

 <VirtualHost *:80> ServerName secondexample.co.zw ServerAlias www.secondexample.co.zw ProxyPreserveHost on ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ RewriteEngine on RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} upgrade [NC] RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L] RewriteCond %{SERVER_NAME} =www.secondexample.co.zw [OR] RewriteCond %{SERVER_NAME} =secondexample.co.zw RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>

The rules are in the wrong order.规则顺序不对。 The canoncial redirect (HTTP to HTTPS) would need to be first.规范重定向(HTTP 到 HTTPS)需要放在首位。 However, if you are wanting to redirect from HTTP to HTTPS then none of the proxy directives are required here and should be removed.但是,如果您想从 HTTP 重定向到 HTTPS,则此处不需要任何代理指令,应将其删除。 Presumbaly the proxy directives are repeated in the <VirtualHost *:443> container.大概代理指令在<VirtualHost *:443>容器中重复。

And if you are only redirecting HTTP to HTTPS then you don't need to use mod_rewrite.如果您只是将 HTTP 重定向到 HTTPS,那么您不需要使用 mod_rewrite。 The simpler mod_alias Redirect directive is preferable.更简单的 mod_alias Redirect指令更可取。 And you should be canonicalising (www vs non-www) the hostname as part of the redirect (unless you are implementing HSTS), not redirecting to the same host.并且您应该将主机名规范化(www 与非 www)作为重定向的一部分(除非您正在实施 HSTS),而不是重定向到同一主机。

For example, it could be simplified to:例如,它可以简化为:

<VirtualHost *:80>
    ServerName secondexample.co.zw
    ServerAlias www.secondexample.co.zw
    Redirect 301 / https://secondexample.co.zw/
</VirtualHost>

All the proxy directives are then in the <VirtualHost *:443> container.然后,所有代理指令都在<VirtualHost *:443>容器中。

The same applies to your first vHost.这同样适用于您的第一个虚拟主机。

The only difference I am noticing is that the second configuration has an alias.我注意到的唯一区别是第二个配置有一个别名。

The Alias makes no difference here. Alias在这里没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM