questions

How to Create a Permanent Redirect Script for Cloaked Links

Rate this post

I am currently using the Yoast cloaking script to create a temporary redirects (302) for affiliate links. For example, if I navigate to https://website.com/go/Site2 it redirects to “https://redirectwebsite2.com”.

How do I adjust the below for permanent redirects (301)? I tried changing the header from 302 to 301 but that doesn’t work.

The following files are saved in the “go” folder in the “public_html” WordPress directory.

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>

index.php

<?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>';
}

redirects.txt

default,https://website.com
Site1,https://redirectwebsite1.com
Site2,https://redirectwebsite2.com

 

✔️Solution:

The solution is to change the header to ( “X-Robots-Tag: noindex, nofollow”, true, 301 )


<?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, 301 );
    header( "Location: " .  $url );
    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>';
}

Leave a Reply

Your email address will not be published.

Back to top button