Last active
June 30, 2018 17:03
-
-
Save pbuyle/9792fcb36689bbf431a0f0c781d0e80a to your computer and use it in GitHub Desktop.
Revisions
-
pbuyle revised this gist
Oct 5, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,12 +29,12 @@ function doDedirects($redirects) { 'cacheDisabled' => FALSE, /* optional, enabled by default */ ]); if (!empty($_SERVER['REQUEST_METHOD']) && !empty($_SERVER['REQUEST_URI'])) { // Fetch method and URI (without the query string) $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); // Match redirect rules and redirects if one is found. $routeInfo = $dispatcher->dispatch($httpMethod, $uri); if ($routeInfo[0] == FastRoute\Dispatcher::FOUND) { $replace = []; -
pbuyle revised this gist
Oct 5, 2016 . 1 changed file with 14 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,18 @@ doDedirects($redirects); /** * Returns a HTTP 301 redirects and die on matching request. * * Match is done only on the request path (without the query string). Sources and their * destinations can contains name pattern used to match muliple paths. For instance, * use 'something/{anything:.*}' as source to match any path below `something/`. And * use 'something/else/{anything}' as its destination to redirect to same path below * 'something/else/`. * * @param $redirects * Redirection rules as an associative array using source as key and destination as values. */ function doDedirects($redirects) { // Collect redirects routes $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) use ($redirects) { @@ -17,13 +29,13 @@ function doDedirects($redirects) { 'cacheDisabled' => FALSE, /* optional, enabled by default */ ]); // Fetch method and URI (without the query string) if (!empty($_SERVER['REQUEST_METHOD']) && !empty($_SERVER['REQUEST_URI'])) { $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); // Match redirect rules and redirects if one if found. $routeInfo = $dispatcher->dispatch($httpMethod, $uri); if ($routeInfo[0] == FastRoute\Dispatcher::FOUND) { $replace = []; foreach ($routeInfo[2] as $key => $value) { -
pbuyle revised this gist
Oct 5, 2016 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ { ... "require": { ... "nikic/fast-route": "^1.0" ... } ... } -
pbuyle revised this gist
Oct 4, 2016 . 2 changed files with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,2 @@ <?php include 'redirects.php'; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ <?php $redirects = [ 'something/{anything:.*}' => 'something-else/{anything}', 'another/one/{anything:.*}' => 'something/else/{anything}', -
pbuyle created this gist
Oct 4, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ include 'redirects.php'; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ $redirects = [ 'something/{anything:.*}' => 'something-else/{anything}', 'another/one/{anything:.*}' => 'something/else/{anything}', ] doDedirects($redirects); function doDedirects($redirects) { // Collect redirects routes $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) use ($redirects) { foreach ($redirects as $source => $destination) { $r->addRoute('GET', $source, $destination); } }, [ 'cacheFile' => sys_get_temp_dir() . '/route.cache', 'cacheDisabled' => FALSE, /* optional, enabled by default */ ]); // Fetch method and URI from somewhere if (!empty($_SERVER['REQUEST_METHOD']) && !empty($_SERVER['REQUEST_URI'])) { $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); if ($routeInfo[0] == FastRoute\Dispatcher::FOUND) { $replace = []; foreach ($routeInfo[2] as $key => $value) { $replace["{{$key}}"] = $value; } $dest = strtr($routeInfo[1], $replace); header("HTTP/1.1 301 Moved Permanently"); header("Location: $dest"); die(); } } }