Преглед изворни кода

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