If for some reason, you don’t want to set up 301 redirects within your application (e.g. using redirect modules or plugins provided by/for popular CMSs) and/or you want to move redirect rules out of the configuration files on your server, moving them up to the load balancer level might be the best option for applications hosted on AWS.
As an example, this might be useful for web applications that run on multiple platforms and/or servers behind a load balancer, which serves as a reverse proxy. In this case, centralizing all redirects in one place might be useful for performance and maintainability reasons. Below are the steps for adding such redirects via AWS console.
Steps for setting up 301 redirect with AWS:
- Login into your AWS console
- Go to EC2 console page
- Go to “Load balancing”–>”Load balancers” in the left sidebar
- 4. Select load balancer you want to set up a redirect on (if you don’t have a load balancer yet see this page https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-application-load-balancer.html)
- Click on the “Listeners” tab
- Click “View/edit rules” on the listener you want to add redirect rules to (if you don’t have any listeners set up yet see this page https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-listener.html)
- Click a “+” tab to add a new rule
- Click a “+ Insert rule” to insert a new rule where you want it (order matters – rules higher up in the list are a higher priority)
- Click “Add condition” in the “IF” column
- Select “Path” and enter the path of the page you want to redirect
- Add any additional conditions if applicable
- Click “Add action” in the “THEN” column
- Select “Redirect to”
- Select protocol, port, “Custom host, path, query” and enter new page path in the “Path” input filed
- Select redirect type you want (301 or 302)
- Click “Save” button
Notes:
AWS doesn’t support Regex Path Based Routing. It only allows using very basic regex-like syntax to define path patterns (see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#path-conditions), but it’s pretty much useless since path can’t be rewritten (so you can’t do something like this ^old/archives/(.*)$ –> /newarchives/$1).
Useful AWS documentation pages:
https://aws.amazon.com/elasticloadbalancing/features/
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html