简体   繁体   中英

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:

<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:

<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. However, if you are wanting to redirect from HTTP to HTTPS then none of the proxy directives are required here and should be removed. Presumbaly the proxy directives are repeated in the <VirtualHost *:443> container.

And if you are only redirecting HTTP to HTTPS then you don't need to use mod_rewrite. The simpler mod_alias Redirect directive is preferable. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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