瀏覽代碼

Add trailing slash redirections

Fixes #13.
Thibaut 12 年之前
父節點
當前提交
dec2f5a249
共有 1 個文件被更改,包括 12 次插入5 次删除
  1. 12 5
      lib/app.rb

+ 12 - 5
lib/app.rb

@@ -118,15 +118,22 @@ class App < Sinatra::Application
     end
   end
 
-  get %r{\A/(.+?)(?=\-|/)} do |doc|
-    return 404 unless @doc = settings.docs[doc]
-    erb :other
-  end
-
   get '/ping' do
     200
   end
 
+  get %r{\A/(\w+)(\-[\w\-]+)?(/)?(.+)?\z} do |doc, type, slash, rest|
+    return 404 unless @doc = settings.docs[doc]
+
+    if !rest && !slash
+      redirect "/#{doc}#{type}/"
+    elsif rest && rest.end_with?('/')
+      redirect "#{doc}#{type}#{slash}#{rest[0...-1]}"
+    else
+      erb :other
+    end
+  end
+
   not_found do
     send_file File.join(settings.public_folder, '404.html'), status: status
   end