Aleks
Aleks

Reputation: 45

Tiny URL that randomly opens a URL from list of URLs?

I'm trying to create a tiny URL that, when clicked, redirects to a randomly selected URL from a pre-determined list of URLs. I know I can achieve this server side with HTML and Javascript, but I want it to function purely from a tiny URL.

Basically how http://www.5050.degstu.com/ does it, but with no host restrictions or URL limit.

I'm pretty much lost with where to begin, and surprised that this doesn't already exist. Is it simply impossible to make for reasons beyond my understanding? I'm still relatively new to the dev world and might not fully grasp the complications involved with this.

Thanks in advance.

Upvotes: 0

Views: 1160

Answers (2)

PMV
PMV

Reputation: 2138

Let's take a look at how 5050 does it if you're interested.

I just made a quick one and logged my connection with fiddler. Here's what I got:

Request:

GET http://5050.degstu.com/v.php?l=qmf72tfeaya5y78 HTTP/1.1
Host: 5050.degstu.com
Connection: keep-alive

(and other headers that aren't really relevant)

Response:

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 03:39:24 GMT
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Location: http://www.vimeo.com

So they're using Option A from my comment - the server keeps some kind of database that matches the "qmf72tfeaya5y78" to the two URLs I gave (YouTube and Vimeo's homepages), and then the server sends back an HTTP 302 to tell my browser to redirect to one of those pages.

Upvotes: 0

alexm
alexm

Reputation: 51

Put this in a script in the page that the TinyURL leads to:

var urls = ['url1', 'url2', 'url3'];

var url = urls[Math.floor(Math.random() * urls.length)];

window.open(url, '_self');

It selects a random URL from a list of URLs, and then opens it in the same tab.

Upvotes: 2

Related Questions