samvermette
samvermette

Reputation: 40437

Redirect Heroku subdomain requests to another IP

I have an app in the App Store that interects with a RESTful Rails app hosted on Heroku. I did the terrible mistake of hardcoding the API's base URL to myapp.heroku.com instead of using the top level domain.

I'm now in the process of migrating to a new server so I'm trying to see what are my options to make the transition seamless to my iPhone app users. I thought of creating a Rake app that redirects all the traffic from that heroku subdomain to my new server address, but then I read that HTTP 1.1 doesn't allow POST redirection (my API having several POST endpoints). I could always redirect my POST requests as GET ones, but that definitely doesn't feel right.

Is there any other option I might be missing? Is there any way Heroku would accept to change the A records of my subdomain to point to my new server IP?

Upvotes: 1

Views: 920

Answers (2)

John Beynon
John Beynon

Reputation: 37507

They wouldn't update their DNS for you - *.heroku.com will be a wildcard DNS entry, they don't add a new subdomain each time a site is added.

It would seem the best solution would be to fix it properly. Attach the new domain to your existing application on Heroku, it will still be accessible on app.heroku.com and yourcustomdomain.com.

Then, update your iOS application to use the new customdomain for it's endpoint. Once it's all working, reduce the TTL on the DNS entry and then repoint it at your new server.

Upvotes: 1

samvermette
samvermette

Reputation: 40437

I ended up doing as John said and change my API endpoint inside my app. To keep previous versions of the app working (which have the heroku subdomain hardcoded in them), I ended up writing this quick Sinatra app and replaced my original Heroku app with it:

require 'sinatra'
require 'mechanize'

API_BASE_URL = "http://newdomain.com"

get '/*' do |path|
    url = URI("#{API_BASE_URL}/#{path}")

    agent = Mechanize.new
    agent.user_agent = request.user_agent

    headers = {'AUTHORIZATION' => request.env['HTTP_AUTHORIZATION']}

    page = agent.post(url, params, headers)

    content_type :json
    page.body
end


get '/*' do |path|
    url = URI("#{API_BASE_URL}/#{path}")

    agent = Mechanize.new
    agent.user_agent = request.user_agent

    headers = {'AUTHORIZATION' => request.env['HTTP_AUTHORIZATION']}

    page = agent.get(url, params, nil, headers)

    content_type :json
    page.body
end

(this code could probably be reduced down to a single method)

Upvotes: 3

Related Questions