Created
May 28, 2018 14:45
-
-
Save shirokoweb/198aa7266f2c948be73774c0dee5a9b5 to your computer and use it in GitHub Desktop.
hide redirection URLS
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 characters
<?php | |
$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 ) ) { | |
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 | |
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false ); | |
if ( $url ) { | |
header( "X-Robots-Tag: noindex, nofollow", true ); | |
header( "Location: " . $url, 302 ); | |
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>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment