Search code examples
amazon-web-servicesamazon-route53aws-application-load-balancer

Redirect all non www HTTPS request to www HTTPS


I want to redirect all requests from https://example.com to https://www.example.com.

I can able to access the application without www and with www.

wwww.example.com is an ALIAS for an application Load Balancer also configured ACM certificate for an application.

Currently, DNS entities configured in Route53.

please find the Network Loadbalancer rule has defined below. But if someone has enter like that https://example.com it doesn't direct to https://www.example.com.

Rules :

Redirect tohttps://#{host}:443/#{path}?#{query}
Status code:HTTP_301

please suggest me how to achieve this scenario.


Solution

  • In the Application Load Balancer you should set up a listener rule on your https listener, i.e. the one listening on port 443, that has a redirect action to change the host from the root to the www subdomain.

    #{host} is the original host, example.com, www.#{host} would therefore be www.example.com

    Redirect to:

    • Protocol: #{protocol}
    • Port: #{port}
    • Custom host: www.#{host}
    • Original path: #{path}
    • Original query: #{query}
    • 301 - Permanently moved

    https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#redirect-actions