Skip to content

Instantly share code, notes, and snippets.

@pbuyle
Last active June 30, 2018 17:03
Show Gist options
  • Select an option

  • Save pbuyle/9792fcb36689bbf431a0f0c781d0e80a to your computer and use it in GitHub Desktop.

Select an option

Save pbuyle/9792fcb36689bbf431a0f0c781d0e80a to your computer and use it in GitHub Desktop.

Revisions

  1. pbuyle revised this gist Oct 5, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions redirects.php
    Original file line number Diff line number Diff line change
    @@ -29,12 +29,12 @@ 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'])) {
    // 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 if found.
    // Match redirect rules and redirects if one is found.
    $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
    if ($routeInfo[0] == FastRoute\Dispatcher::FOUND) {
    $replace = [];
  2. pbuyle revised this gist Oct 5, 2016. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions redirects.php
    Original 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 from somewhere
    // 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) {
  3. pbuyle revised this gist Oct 5, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    {
    ...
    "require": {
    ...
    "nikic/fast-route": "^1.0"
    ...
    }
    ...
    }
  4. pbuyle revised this gist Oct 4, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions end-of-settings.php
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    <?php
    include 'redirects.php';
    1 change: 1 addition & 0 deletions redirects.php
    Original 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}',
  5. pbuyle created this gist Oct 4, 2016.
    1 change: 1 addition & 0 deletions end-of-settings.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    include 'redirects.php';
    37 changes: 37 additions & 0 deletions redirects.php
    Original 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();
    }
    }
    }