Archive for May, 2012

Nginx and Apache rewrites to support HTML5 Pushstate in pure Backbone.js (or other JS MV*) Application.

Posted on May 17th, 2012 in Apache, backbone, nginx | 4 Comments »

HTML5 pushstate is awesome. It enables you to change the URL of your site dynamically without refreshing the page (goodbye hashes!). Libraries like Backbone have great support for this. Unfortunately if a user bookmarks or refresh a page on an app that’s using HTML5 pushstate, it makes a request to the server for that deep linked content. Here are the rewrites for Nginx and Apache to internally redirect that call to the same html file. Browser thinks its a unique page but it’s the same.

Apache

In your vhost :

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

Nginx

    rewrite ^(.+)$ /index.html last;

Note that once you have this in place your server no longer reports 400 errors as all requests pull up the index page. To work around this you can create a 404 in a Backbone route:

  routes: {
    // Other routes
    "*path"  : "notFound"
  },
 
  notFound: function(path) {
    // Load 404 template, probably of a cute animal.
  }

405 Not Allowed on Facebook Canvas Page

Posted on May 11th, 2012 in facebook, nginx | 2 Comments »

I enabled Facebook Canvas for a responsive web app we’re building an noticed despite meeting the SSL requirement and hitting the correct page, that Nginx was returning a 405 Not Allowed.

Turns out Facebook makes a POST request to your HTML page and you need to allow this. Here’s the Nginx code:

location / {
    error_page 405 =200 $uri;
    root /var/www/html/yoursite.com;
}