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]
(source: http://backbonetutorials.com/seo-for-single-page-apps/)

` How can I do this in Heroku?

Answers


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 https://github.com/ryandotsmith/nginx-buildpack

Step 1: Use heroku-buildpack-multi
$ heroku config:set BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
Step 2: Add the required extra buildpacks to .buildpacks
$ echo 'https://github.com/ryandotsmith/nginx-buildpack.git' >> .buildpacks
$ echo 'https://github.com/heroku/heroku-buildpack-python.git' >> .buildpacks
$ git commit .buildpacks -m 'Add multi-buildpack'
Step 3: Add uwsgi to your requirements.py. 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
[uwsgi]
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

use webkit-playsinline in javascript

javascript html5 video

How do I use webkit-playsinline in javascript instead of in html5 video tag? I want to use it just like using video tag control/autoplay attribute in javascript or if you guys have any other method...

How to get rid of .html extension when serving webpages with node.js?

html node.js

I am a beginner with node.js and am using express with the ejs layout, and I want to know how to get rid of the .html extension when putting up a page. For example if I go to my localhost:3000/abou...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.