Skip to content

Commit

Permalink
Tell (Google)bots not to index short URLs
Browse files Browse the repository at this point in the history
Should fix YOURLS#2202
  • Loading branch information
ozh committed Aug 5, 2018
1 parent e0e3d5e commit 364c03a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
28 changes: 28 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,34 @@ function yourls_redirect( $location, $code = 301 ) {
die();
}

/**
* Redirect to an existing short URL
*
* Redirect client to an existing short URL (no check performed) and execute misc tasks: update
* clicks for short URL, update logs, and send a nocache header to prevent bots indexing short
* URLS (see #2202)
*
* @since 1.7.3
* @param string $url
* @param string $keyword
*/
function yourls_redirect_shorturl($url, $keyword) {
yourls_do_action('redirect_shorturl', $url, $keyword);

// Update click count in main table
$update_clicks = yourls_update_clicks($keyword);

// Update detailed log for stats
$log_redirect = yourls_log_redirect($keyword);

// Tell (Google)bots not to index this short URL, see #2202
if( !headers_sent() ) {
header("X-Robots-Tag: noindex", true);
}

yourls_redirect($url, 301);
}

/**
* Set HTTP status header
*
Expand Down
12 changes: 2 additions & 10 deletions yourls-go.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@

// URL found
if( !empty( $url ) ) {
yourls_do_action( 'redirect_shorturl', $url, $keyword );

// Update click count in main table
$update_clicks = yourls_update_clicks( $keyword );

// Update detailed log for stats
$log_redirect = yourls_log_redirect( $keyword );

yourls_redirect( $url, 301 );
yourls_redirect_shorturl($url, $keyword);

// URL not found. Either reserved, or page, or doesn't exist
} else {
Expand All @@ -38,7 +30,7 @@
// Either reserved id, or no such id
} else {
yourls_do_action( 'redirect_keyword_not_found', $keyword );

yourls_redirect( YOURLS_SITE, 302 ); // no 404 to tell browser this might change, and also to not pollute logs
}
}
Expand Down

0 comments on commit 364c03a

Please sign in to comment.