Random URL Redirect and Site Discovery Script
Random Link Generator redirects visitors to a random URL from your configured database. When clicked, users are taken to a randomly selected site from your list. Perfect for link rotators, random site discovery, and partner link distribution.
Random Link Generator allows you to create a "random site" link that, when clicked, redirects visitors to a randomly selected URL from your database. The URLs are stored in a simple text file, making it easy to add or remove links.
When a visitor clicks the link to the script, it reads the URL database file, randomly selects one URL, and sends an HTTP redirect to that URL. The visitor is instantly taken to the random destination.
| File | Description |
|---|---|
rand_link.pl |
Main Perl script that performs the random redirect |
rand_link.html |
Sample HTML page showing how to call the script |
rand_log |
Optional log file for tracking usage |
README |
Installation instructions and configuration guide |
Each click selects a truly random URL from your database with equal probability.
URLs stored in a plain text file, one per line. Easy to edit and maintain.
Track which URLs are accessed and when for analytics and monitoring.
Uses HTTP 302 redirect for instant redirection to the destination.
Works with Free For All Link pages for automatic database updates.
Minimal setup required - just configure the database file path.
The URL database is a simple text file with one URL per line:
links.txt - Example Database
https://example1.com/
https://example2.com/path/to/page.html
https://example3.org/
http://legacy-site.com/old-page.html
https://partner-site.net/landing
rand_link.pl to your cgi-bin directory.
#!/usr/bin/perl).
$linkfile variable to the path of your URL database.
chmod 755 rand_link.pl
<a href="/cgi-bin/rand_link.pl">Random Site</a> to your pages.
#!/usr/bin/perl
use strict;
use warnings;
# Configuration
my $linkfile = '/var/www/data/links.txt';
my $logfile = '/var/www/logs/rand_link.log';
my $enable_logging = 1;
# Read links from file
open(my $fh, '<', $linkfile) or die "Cannot open $linkfile: $!";
my @links = grep { /^https?:\/\// } map { chomp; $_ } <$fh>;
close($fh);
# Check if we have any links
if (!@links) {
print "Content-type: text/html\n\n";
print "No links available.";
exit;
}
# Select random link
my $url = $links[int(rand(@links))];
# Log the access (optional)
if ($enable_logging && $logfile) {
if (open(my $log, '>>', $logfile)) {
my $time = localtime();
my $ip = $ENV{REMOTE_ADDR} || 'unknown';
my $referer = $ENV{HTTP_REFERER} || 'direct';
print $log "$time | $ip | $referer | $url\n";
close($log);
}
}
# Redirect to the random URL
print "Status: 302 Found\n";
print "Location: $url\n\n";
exit 0;
<?php
/**
* Random Link Generator - PHP Version
*/
// Configuration
$linkfile = '/var/www/data/links.txt';
$logfile = '/var/www/logs/rand_link.log';
$enable_logging = true;
// Read links from file
$links = file($linkfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($links === false || empty($links)) {
die('No links available.');
}
// Filter to valid URLs only
$links = array_filter($links, function($url) {
return filter_var($url, FILTER_VALIDATE_URL);
});
if (empty($links)) {
die('No valid links available.');
}
// Select random link
$url = $links[array_rand($links)];
// Log the access (optional)
if ($enable_logging && $logfile) {
$time = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$referer = $_SERVER['HTTP_REFERER'] ?? 'direct';
$log_entry = "$time | $ip | $referer | $url\n";
file_put_contents($logfile, $log_entry, FILE_APPEND | LOCK_EX);
}
// Redirect to the random URL
header('Location: ' . $url, true, 302);
exit;
?>
<?php
/**
* Weighted Random Link Generator
* Links with higher weights appear more often
*/
// Format: URL|weight
$links = [
'https://premium-partner.com/' => 50,
'https://gold-partner.com/' => 30,
'https://silver-partner.com/' => 15,
'https://bronze-partner.com/' => 5,
];
function weightedRandom($items) {
$total = array_sum($items);
$rand = mt_rand(1, $total);
$current = 0;
foreach ($items as $url => $weight) {
$current += $weight;
if ($rand <= $current) {
return $url;
}
}
return array_key_first($items);
}
$url = weightedRandom($links);
header('Location: ' . $url, true, 302);
exit;
?>
/**
* Random Link Generator - JavaScript Version
* Client-side random redirect with no server required
*/
class RandomLinkGenerator {
constructor(links, options = {}) {
this.links = links;
this.options = {
openInNewTab: true,
trackClicks: false,
...options
};
}
getRandomLink() {
return this.links[Math.floor(Math.random() * this.links.length)];
}
redirect() {
const url = this.getRandomLink();
if (this.options.trackClicks) {
this.trackClick(url);
}
if (this.options.openInNewTab) {
window.open(url, '_blank', 'noopener,noreferrer');
} else {
window.location.href = url;
}
}
trackClick(url) {
// Send to analytics
console.log('Random link clicked:', url);
// navigator.sendBeacon('/track/click', JSON.stringify({ url: url }));
}
// Bind to a button/link element
bindTo(selector) {
const element = document.querySelector(selector);
if (element) {
element.addEventListener('click', (e) => {
e.preventDefault();
this.redirect();
});
}
}
}
// Usage examples:
// Basic usage
const links = [
'https://example1.com/',
'https://example2.com/',
'https://example3.com/',
'https://example4.com/',
];
const randomizer = new RandomLinkGenerator(links);
// Bind to a button
randomizer.bindTo('#random-link-button');
// Or redirect immediately
// randomizer.redirect();
// With weighted links
class WeightedRandomLinks {
constructor(weightedLinks) {
this.links = weightedLinks;
this.totalWeight = weightedLinks.reduce((sum, item) => sum + item.weight, 0);
}
getRandomLink() {
let random = Math.random() * this.totalWeight;
for (const item of this.links) {
random -= item.weight;
if (random <= 0) {
return item.url;
}
}
return this.links[0].url;
}
redirect() {
window.location.href = this.getRandomLink();
}
}
// Weighted usage
const weightedLinks = [
{ url: 'https://premium.com/', weight: 50 },
{ url: 'https://gold.com/', weight: 30 },
{ url: 'https://silver.com/', weight: 15 },
{ url: 'https://bronze.com/', weight: 5 },
];
const weighted = new WeightedRandomLinks(weightedLinks);
// HTML inline usage
// <a href="#" onclick="randomizer.redirect(); return false;">Random Site</a>
<!-- Basic link to CGI script -->
<a href="/cgi-bin/rand_link.pl">Visit a Random Site</a>
<!-- Styled button -->
<a href="/cgi-bin/rand_link.pl" class="btn btn-primary">
<i class="bi bi-shuffle"></i> Random Site
</a>
<!-- Open in new tab -->
<a href="/cgi-bin/rand_link.pl" target="_blank" rel="noopener">
Random Site (New Tab)
</a>
<!-- JavaScript approach (recommended for modern sites) -->
<button id="random-link-btn" class="btn btn-success">
<i class="bi bi-shuffle"></i> Discover Random Site
</button>
<script>
const sites = [
'https://example1.com/',
'https://example2.com/',
'https://example3.com/'
];
document.getElementById('random-link-btn').addEventListener('click', function() {
const url = sites[Math.floor(Math.random() * sites.length)];
window.open(url, '_blank', 'noopener');
});
</script>
<!-- Bootstrap card with random link feature -->
<div class="card">
<div class="card-body text-center">
<h5 class="card-title">Feeling Lucky?</h5>
<p class="card-text">Discover a random site from our collection!</p>
<a href="/cgi-bin/rand_link.pl" class="btn btn-lg btn-warning">
<i class="bi bi-dice-5"></i> Take Me Somewhere Random
</a>
</div>
</div>
Utility scripts to help manage your link database:
Converts Free For All link pages to Random Link database format. Extracts URLs from HTML and creates a clean link file.
make_list.pl
Converts Netscape bookmark files to Random Link database format. Turn your bookmarks into a random discovery tool.
convert.pl
Main Perl script for random URL redirection
Sample HTML page with usage examples
Optional log file template for tracking
Installation and configuration guide
https://example1.com/
https://example2.com/page.html
https://example3.org/
make_list.pl converts Free For All link pages to the database format, and convert.pl converts Netscape bookmark files. You can also use simple grep/sed commands or write a custom script to extract URLs from HTML.
URL|weight and implement weighted random selection (see code examples above).