0

I have a ruby on rails application setup on AWS elastic beanstalk using nginx and puma, my requirement is to redirect all my requests from http://example.com or http://www.example.com or https://example.com to https://www.example.com. Also I don't want request from my subdomains to be redirected to www, like I don't want http://subdomain.example.com to be redirected to https://www.expample.com or https://www.subdomain.example.com.

Using the link

https://stackoverflow.com/questions/24297375/how-to-get-elastic-beanstalk-nginx-backed-proxy-server-to-auto-redirect-from-htt

I was able to redirect all my requests from http to https but this does not redirect non www to www requests.

  • This should be probably posted on serverfault. Apart from that, you need to create a virtual server for each domain you want to redirect and redirect it permanently to where you want it to go. – Stavros Souvatzis Dec 31 '16 at 08:04
  • Possible duplicate of [nginx redirect to www.domain](http://serverfault.com/questions/502026/nginx-redirect-to-www-domain) – Tero Kilkanen Dec 31 '16 at 15:24
  • @TeroKilkanen the link you provided does not solve my problem entirely, I need a solution for a Elastic beanstalk server using nginx and not just nginx (you could refer to the link provided in question for the type of solution I need) – chinar_reg Jan 02 '17 at 05:41

1 Answers1

0

I do this in my routes.rb file:

# Redirect example.com => www.example.com
constraints(host: /^example\.com/i) do
  match "/(*path)" => redirect { |params, _req| "https://www.example.com/#{params[:path]}" }, via: [:get, :post]
end

This will for both non-secure and secure connections to https, but if you really need to keep http requests as non-secure, then you could have a more specific constraint.

Brian
  • 201
  • 1
  • 3