Seb
Seb

Reputation: 1113

How to map /subfolder to a different server with AWS Cloudfront?

I have a wordpress site and a dynamic webapp that need to be served from the same domain. I want to avoid using a subdomain or redirect due to SEO reasons.

Here is what I'd want to achieve:

mydomain.com/*            -> 192.168.2.1   // WP installation: anything BUT /subfolder
mydomain.com/subfolder/*  -> 192.168.2.2   // Web app: anything IN /subfolder

I read here in a comment that AWS CloudFront "can map different paths to different servers"

However I struggle to find any evidence, tutorial or confirmation if it's possible or how to do that.

Can the above setup be realized with Cloudfront? If so - how?

Upvotes: 0

Views: 867

Answers (1)

Seb
Seb

Reputation: 1113

OK - I ended up using cloudflare instead as it was very simple.

Basically once your site is running via cloudfront you can create a worker to handle all routes to the subdirectory:

addEventListener('fetch', event => {
  var url = new URL(event.request.url);
  if (url.pathname.startsWith('/subfolder/') || url.pathname === '/subfolder') {
    handleBlog(event, url);
  } else {
    event.respondWith(fetch(event.request));
  }
})

async function handleBlog(event, url) {
  var originUrl = url.toString().replace(
    'https://www.example.com/subfolder',
   'https://example.org/');
  event.respondWith(fetch(originUrl)); 
}

Upvotes: 1

Related Questions