Apache2 virtualhosts, changing domain, with redirect AND Proxy with SSL

22 Views Asked by At

I'm migrating from a.com to b.com

I have some hardware in the field that is making POST requests to web.a.com and I can't really change that.

I have my code in /var/www/web.b.com working with SSL.

When I visit https://web.a.com in the browser, I want it to redirect to https://web.b.com.

I'm also redirecting http to https, which works great.

Here's my apache site config:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName web.a.com

    RewriteEngine On

    # Redirect all HTTP requests to web.b.com while preserving headers and POST data
    RewriteRule ^ http://web.b.com%{REQUEST_URI} [R=307,QSA,L,P]

    # Redirect all HTTPS requests to web.b.com while preserving headers and POST data
    RewriteCond %{HTTPS} on
    RewriteRule ^ https://web.b.com%{REQUEST_URI} [R=307,QSA,L,P]
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName web.a.com

    RewriteEngine On

    # Enable SSL proxy support
    SSLProxyEngine On

    # Rewrite the URL to https://web.b.com
    RewriteRule ^ https://web.b.com%{REQUEST_URI} [R=307,QSA,L,P]

    # Proxy all HTTPS requests to web.b.com while preserving headers and POST data
    ProxyPass / https://web.b.com/
    ProxyPassReverse / https://web.b.com/

    # Rewrite the URL to https://web.b.com
    # RewriteRule ^ https://web.b.com%{REQUEST_URI} [R=307,QSA,L,P]

    # Rewrite the URL to https://web.b.com
    # RewriteCond %{HTTP_HOST} ^web\.a\.com$
    # RewriteRule ^(.*)$ https://web.b.com$1 [R=307,QSA,L,P]

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/a.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/a.com/privkey.pem
</VirtualHost>

The proxying flag P seems to allow the headers to get passed through when there's a request from web.a.com, right through to web.b.com, which works great. The 307 makes the POST request come through as POST still, which works great.

However, when I visit https://web.a.com in the browser, the page loads with the https://web.a.com URL, instead of redirecting to https://web.b.com.

How can I redirect, while passing through the whole request including headers? Is there a better solution?

Edit: I'm wondering, is it fundamentally not possible to do, with the way that HTTP works? Perhaps the client (upon receiving a redirect to a new URL) might not immediately retry the same request with the same headers?

0

There are 0 best solutions below