I’ll describe the current setup first followed by what I’d like to change it to.
Current Setup
- Primary domain www.example.com pointing to a Ruby on Rails app deployed on Heroku.
- WordPress blog blog.example.com running on Flywheel.
- RoR application serves as reverse-proxy to load Flywheel content under /blog subdirectory. For ex. www.example.com/blog/page1 loads content from blog.spacebox.com/page1. We’re using https://github.com/waterlink/rack-reverse-proxy for making this work.
All of the above works as expected.
What We Want
Now the marketing team, wanting to up the SEO game, wants blog.example.com/* to redirect to www.example.com/blog/*
What I’ve Tried So Far (with failure, of course)
- Setup a Redirect on Flywheel dashboard to www.example.com/blog/$1 (expectedly resulted in infinite redirects).
- Deleted A record from DNSSimple for blog.example.com (pointing to Flywheel IP address), and replaced with a CNAME to point to www.example.com (didn’t work).
With the above given stack, how can the desired result be achieved? What you recommend a stack change to achieve the outcome?
Much thanks in advance, as I’ve spent a good few days trying to make this work.
2
Answers
To redirect a subdomain to a subdirectory in a primary domain serving as a reverse-proxy, you can use the following steps:
1.Configure your DNS records. You will need to create a CNAME record for your subdomain that points to the IP address of your primary domain. Configure your reverse proxy. You will need to configure your reverse proxy to forward requests to your subdomain to the subdirectory in your primary domain. For example, if your subdomain is blog.example.com and you want to redirect it to the /blog subdirectory of your primary domain, then you would configure your reverse proxy to forward requests to blog.example.com to example.com/blog.
2.Configure a redirect in your primary domain. You will need to configure a redirect in your primary domain to redirect all requests to the subdomain to the subdirectory. For example, if your primary domain is example.com and you want to redirect all requests to blog.example.com to the /blog subdirectory, then you would configure a redirect in example.com to redirect all requests to /blog.
Once you have configured your DNS records, your reverse proxy, and your redirect, then all requests to your subdomain will be redirected to the subdirectory in your primary domain.
Assuming you actually want a redirect, this is actually pretty simple and can be handled in Rails routes.
Every get request that comes to the blog subdomain matches this route and will be redirected to the corresponding
/blog
path.Check the Rails routing documentation for more details.