+// Rewrite chapter urls using the canonical name. e.g ch01.html becomes introduction.html
+(function ($) {
+ if (document.location.pathname.match(/\/ch\d+\.html$/)) {
+ var url = document.location.href;
+ var canonical = $('link[rel="canonical"]').attr('href');
+ if (canonical) {
+ canonical = canonical.replace(/^[^#]+(\/[^\/#]+).*/,'$1');
+ url = url.replace(/\/ch\d+\.html/, canonical);
+ if ("history" in window && "replaceState" in window.history)
+ window.history.replaceState("", document.title, url);
+ else
+ document.location.href = url;
+ }
+ }
+})(jQuery);
+