AWS CloudFront – How to Redirect Root Domain Requests

amazon s3amazon-cloudfrontamazon-lambdaamazon-web-services

First time poster on serverfault, apologies if this is in the wrong place. I have a website that I am trying to set up on AWS CloudFront. It is currently configured to serve static web content from S3, this is then picked up and served via CloudFront. However, I want all of the requests for rootdomain.com to go to rootdomain.com/blog – and the rest of the requests for that domain to be handled as normal.

From my Googling so far, this seems like something that I could achieve using lambda@edge. However, all of my attempts so far have been unsuccessful.

How do I go about configuring lambda/cf/s3 to achieve what I have laid out above?

Apologies if I have missed anything, thanks very much in advance.

Best Answer

If you only want / redirected to /blog you can do a simple index.html based redirect. Lambda@Edge is a very powerful mechanism but very much an overkill for your usecase.

To use index.html for the redirect do this:

  1. Configure the S3 bucket for website hosting, that will also make sure that a request to / will serve the contents of /index.html.

  2. Create and upload the /index.html file with a content like this:

    <html>
    <head>
    <title>Redirect to /blog</title>
    <meta http-equiv="refresh" content="0; url=/blog" />
    </head>
    <body>
    </body>
    </html>
    

Now every time a request is made to http://rootdomain.com/ the browser will receive the above HTML file and follow the refresh/redirect to http://rootdomain.com/blog.

Hope that helps :)