]> git.agnieray.net Git - galette.git/commitdiff
Drop trailing slash
authorJohan Cwiklinski <johan@x-tnd.be>
Sat, 4 Mar 2017 13:09:24 +0000 (14:09 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Sat, 4 Mar 2017 13:09:24 +0000 (14:09 +0100)
galette/includes/main.inc.php

index bf56d01d49b0ffb626183f7d5b5a1b54a61dccd5..e26a6a7f14166245a599355b639d4d6c83af9da1 100644 (file)
@@ -321,6 +321,27 @@ function getCurrentRoute($app)
     return $cur_route;
 }
 
+/**
+ * Trailing slash middleware
+ */
+$app->add(function ($request, $response, $next) {
+    $uri = $request->getUri();
+    $path = $uri->getPath();
+    if ($path != '/' && substr($path, -1) == '/') {
+        // permanently redirect paths with a trailing slash
+        // to their non-trailing counterpart
+        $uri = $uri->withPath(substr($path, 0, -1));
+
+        if ($request->getMethod() == 'GET') {
+            return $response->withRedirect((string)$uri, 301);
+        } else {
+            return $next($request->withUri($uri), $response);
+        }
+    }
+
+    return $next($request, $response);
+});
+
 /**
  * This is important this one to be the last, so it'll be executed first.
  */