Redirect bots using heroku to a different server

I have a Django website running on Heroku where most of the data is served via AJAX. In order to allow search engines to index those pages I want to serve requests with the _escaped_fragment url parameter using phantomjs running on a separate nodejs server. similar to this on apache:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule (.*) http://webserver:3000/%1? [P]

` How can I do this in Heroku?


Heroku does not give you control over the frontend loadbalancer (which is where you'd ideally want this embedded). You would need to embed this logic into your application (See: Middleware), or run your own "mini loadbalancer" in front of your application (i.e. you would run nginx on each web worker, purely to implement this match and redirect scheme).

To use nginx, the fastest way is to use

Step 1: Use heroku-buildpack-multi
$ heroku config:set BUILDPACK_URL=
Step 2: Add the required extra buildpacks to .buildpacks
$ echo '' >> .buildpacks
$ echo '' >> .buildpacks
$ git commit .buildpacks -m 'Add multi-buildpack'
Step 3: Add uwsgi to your UWSGI is the fastest way to server python projects out of heroku, and importantly it can listen on a domain socket.
Step 4 : Add a uwsgi.conf to your project
http-socket = /tmp/nginx.socket
master = true
processes = 4
die-on-term = true
memory-report = true
module = yourapp.wsgi:application
env = DJANGO_SETTINGS_MODULE=yourapp.settings
Step 5: Create config/nginx.conf.erb in your project. Copy from here. Edit this to add your custom redirect logic.
Step 6 : Modify your app to touch /tmp/app-initialized when it starts up

Now obviously this is pretty heavy modification, and will take a while to get it set up right. If you don't really need this high-performance option, take the simpler route and set up a custom middleware layer instead.

Need Your Help

How to display an image if browser doesn't support HTML 5 Canvas

javascript html html5 internet-explorer-8 html5-canvas

I have some HTML 5 content with Javascript which generates a set of rectangles and texts with links.

if you reimplemented twitter, what would you do differently?

ruby-on-rails ruby architecture twitter microblogging

I just saw the hilarious "The Rise and Fall of Twitter" which made me think: