So, I am trying to put a WP website hosted outside of AWS behind a AWS CloudFront distribution. Now for custom origin AWS CloudFront wants you to enter the domain of the origin server. So, my DNS is set like this:
Cname example.com points to cloudfront distribution domain
A Record origin.example.com points to the IP of my origin server.
Now on my origin server which is a WordPress site, site_url is set to example.com. - in this case, when CloudFront tries to access my origin using origin.example.com it gets redirected to example.com, which results in too many redirects error. If I change the site_url to origin.example.com, then I get the homepage correct but hostname to all the other linked resources is set to origin.example.com which beats the purpose of using CloudFront.
So, I have to keep the site_url set to example.com but also want to access the site using origin.exmaple.com with the condition that WordPress should build the links using example.com hostname.
I tried hostname rewrite via .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [PT]
But it is redirecting the request. I need a way so that the request is not redirected but hostname is replaced for the same request. Or maybe some other way to use CloudFront in front of WordPress site as the origin server.