Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Last active January 7, 2025 07:26
Show Gist options
  • Save jdevalk/5622742 to your computer and use it in GitHub Desktop.
Save jdevalk/5622742 to your computer and use it in GitHub Desktop.

Revisions

  1. Joost de Valk revised this gist May 22, 2013. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    $id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $f = fopen( 'redirects.txt', 'r' );
    $urls = array();

    // The file didn't open correctly.
    @@ -12,7 +12,12 @@

    // Read the input file and parse it into an array
    while( $data = fgetcsv( $f ) ) {
    $urls[ $data[0] ] = $data[1];
    if ( !isset( $data[0] ) || !isset( $data[1] ) )
    continue;

    $key = trim( $data[0] );
    $val = trim( $data[1] );
    $urls[ $key ] = $val;
    }

    // Check if the given ID is set, if it is, set the URL to that, if not, default
  2. Joost de Valk revised this gist May 22, 2013. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,27 @@
    <?php

    $id = isset( $_GET['id'] ) ? $_GET['id'] : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $urls = array();

    // The file didn't open correctly.
    if ( !$f ) {
    echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
    die;
    }

    // Read the input file and parse it into an array
    while( $data = fgetcsv( $f ) ) {
    $urls[ $data[0] ] = $data[1];
    }

    // Check if the given ID is set, if it is, set the URL to that, if not, default
    $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : '' );
    $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );

    header( "X-Robots-Tag: noindex, nofollow", true );
    if ( !empty( $url ) ) {
    if ( $url ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " . $url, 302 );
    die;
    die;
    } else {
    echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
    <pre>default,http://example.com</pre>
  3. Joost de Valk revised this gist May 22, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php

    $id = isset( $_GET['id'] ) ? $_GET['id'] : 'default';
    $id = isset( $_GET['id'] ) ? $_GET['id'] : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $urls = array();

    @@ -21,5 +21,9 @@
    header( "X-Robots-Tag: noindex, nofollow", true );
    if ( !empty( $url ) ) {
    header( "Location: " . $url, 302 );
    die;
    die;
    } else {
    echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
    <pre>default,http://example.com</pre>
    <p>Where you should replace example.com with your domain.</p>';
    }
  4. Joost de Valk revised this gist May 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,8 @@
    // Check if the given ID is set, if it is, set the URL to that, if not, default
    $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : '' );

    header( "X-Robots-Tag: noindex, nofollow", true );
    if ( !empty( $url ) ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " . $url, 302 );
    die;
    }
  5. Joost de Valk revised this gist May 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@
    // Check if the given ID is set, if it is, set the URL to that, if not, default
    $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : '' );

    if ( isset( $url ) ) {
    if ( !empty( $url ) ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " . $url, 302 );
    die;
  6. Joost de Valk revised this gist May 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion index.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    $id = isset( $_GET['id'] ) ? $_GET['id'] : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $f = fopen( 'redirects.txt', 'r' );
    $urls = array();

    // The file didn't open correctly.
  7. Joost de Valk created this gist May 21, 2013.
    5 changes: 5 additions & 0 deletions .htaccess
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^index\.php$ - [L]
    RewriteRule (.*) ./index.php?id=$1 [L]
    </IfModule>
    25 changes: 25 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php

    $id = isset( $_GET['id'] ) ? $_GET['id'] : 'default';
    $f = fopen( 'redirects.txt', 'r' );
    $urls = array();

    // The file didn't open correctly.
    if ( !$f ) {
    echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
    die;
    }

    // Read the input file and parse it into an array
    while( $data = fgetcsv( $f ) ) {
    $urls[ $data[0] ] = $data[1];
    }

    // Check if the given ID is set, if it is, set the URL to that, if not, default
    $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : '' );

    if ( isset( $url ) ) {
    header( "X-Robots-Tag: noindex, nofollow", true );
    header( "Location: " . $url, 302 );
    die;
    }
    3 changes: 3 additions & 0 deletions redirects.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    default,http://example.com
    yoast,http://yoast.com
    test,http://yoast.com/?p=2