Browse Source

Tell (Google)bots not to index short URLs

Should fix #2202
pull/2435/head
ozh 1 year ago
parent
commit
364c03a5f7
2 changed files with 30 additions and 10 deletions
  1. 28
    0
      includes/functions.php
  2. 2
    10
      yourls-go.php

+ 28
- 0
includes/functions.php View File

@@ -720,6 +720,34 @@ function yourls_redirect( $location, $code = 301 ) {
}

/**
* 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
*
* @since 1.4

+ 2
- 10
yourls-go.php View File

@@ -18,15 +18,7 @@ $url = yourls_get_keyword_longurl( $keyword );

// 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 {
@@ -38,7 +30,7 @@ if( !empty( $url ) ) {
// 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
}
}

Loading…
Cancel
Save