TinyUrl

  • Thread starter Thread starter shapper
  • Start date Start date
I don't think they have a web api but it would be fairly simple to create
your own URL aliasing service.
 
I don't think they have a web api but it would be fairly simple to create
your own URL aliasing service.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

They have something like:
http://tinyurl.com/api-create.php?url=http://www.myurl.com

And there is also other systems like: Bit.ly

My own URL aliasing service?! Well, that is something I didn't think
off! But now that you say that ... lol

Maybe I would create an SQL table with 3 fields:
AliasId
AliasUrl
SourceUrl

For example:
http://www.mywebsite.com/document/show/ade59599-8686-49ab-b64d-35be3957e6aa
would become:

http://mywebsite.com/a1

I believe in most applications I will only need a 2 characters length:
26 letters + 10 digits = 2 ^ 36 combinations = 68 719 476 736

Just two questions:
1. What do you think about my implementation (including the SQL
table):
Maybe I could even change it to:
AliasKey
SourceUrl

Since AliasKey is always unique it can be used as PK and then I
just add the basic url: http://mywebsite.com/ + AliasKey

2. How can I generate a 2 character string using 2 of the 26 letters +
10 digits and be sure that does not exist in the table yet?

Thanks,
Miguel
 
shapper said:
I believe in most applications I will only need a 2 characters length:
26 letters + 10 digits = 2 ^ 36 combinations = 68 719 476 736

My maths is generally rusty, and often wrong, but I’m not sure you’ve
got this correct. Shouldn’t it be 36^2 (rather than 2^36) and therefore
be only 1296 combinations?
 
Dylan said:
My maths is generally rusty, and often wrong, but I’m not sure you’ve
got this correct. Shouldn’t it be 36^2 (rather than 2^36) and therefore
be only 1296 combinations?

Furthermore, you’d need a string of at least 7 characters to get close
to the number of combinations you suggested above, ie. 36^7 =
78,364,164,096.
 
Furthermore, you’d need a string of at least 7 characters to get close
to the number of combinations you suggested above, ie. 36^7 =
78,364,164,096.

Obviously ... I was calculating nAp and instead of making n^p I made
p^n ...
That was a mistake when writing fast my post ... Shame ...I am a Math
College teacher ... lol
So I will increase the length and may drop a few more characters
there ...

Anyway, math is not really the problem.

The problem is:
How to generate a random key and be sure that it is unique.

Thanks,
Miguel
 
shapper said:
The problem is:
How to generate a random key and be sure that it is unique.

I think you may be looking at the problem from the wrong angle here. If
you’re going to be storing the keys in a database, then it probably
makes sense to get the database to generate the unique key.

That’s as helpful as I can be though as I don’t know enough about
databases to be able to get you any further. I’d suggest asking in an
SQL newsgroup as to how you can generate an ID matching your criteria.
 
Obviously ... I was calculating nAp and instead of making n^p I made
p^n ...
That was a mistake when writing fast my post ... Shame ...I am a Math
College teacher ... lol
So I will increase the length and may drop a few more characters
there ...

Anyway, math is not really the problem.

The problem is:
How to generate a random key and be sure that it is unique.

Thanks,
Miguel

you can use a guid for that
 
you can use a guid for that

A Guid?!

I am trying to create url alias associated to resources on my
application. So:
http://www.mywebsite.com/article/show/c2f1bc05-756e-4096-b4dc-abc7b92e1214

would have an url alias:
http://mywebsite.com/4ikjh

So on a SQL database I need to create a record that associates the
short url to the resource:
Resource = "article/show/c2f1bc05-756e-4096-b4dc-abc7b92e1214"
Alias = "4ikjh"

But when I create this record I need to be sure that there isn't any
other alias equal to 4ikjh.

So basically, Alias, could also work as a PK since it will need to be
unique.

But having Alias to be a Guid makes no sense ... it does not short the
url much ...

Do you understand?



I want to create a short url ... so I am creating keys like: cjk21.
 
Does anyone know how to create a TinyUrl from a url using c#?

Since URL should be unique, why not just generate a hash of the URL and use
it?
 
Since URL should be unique, why not just generate a hash of the URL and use
it?

You mean from:
http://www.mywebsite.com/document/show/ade59599-8686-49ab-b64d-35be39

Hash the part:
document/show/ade59599-8686-49ab-b64d-35be39

And add it to http://mywebsite.com to become
http://mywebsite.com/$HashResult$

Then I would place this HashResult on my Alias table associated to the
resource.

HashResult will be always unique?
And can I make it 4 characters length?
Because if it will be very long then I will not get a shorter url ...

Thanks,
Miguel
 
You mean from:http://www.mywebsite.com/document/show/ade59599-8686-49ab-b64d-35be39

Hash the part:
document/show/ade59599-8686-49ab-b64d-35be39

And add it tohttp://mywebsite.comto becomehttp://mywebsite.com/$HashResult$

Then I would place this HashResult on my Alias table associated to the
resource.

HashResult will be always unique?
And can I make it 4 characters length?
Because if it will be very long then I will not get a shorter url ...

Thanks,
Miguel

For example, on my project I have the following to hash a string to
send the user so it can confirm its account:

public static String Hash(String text) {
MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider
();
Byte[] hash = null;
UTF8Encoding encoder = new UTF8Encoding();
hash = crypto.ComputeHash(encoder.GetBytes(text));
return BitConverter.ToString(hash).Replace("-", String.Empty);
} // Hash

But I don't think I can control the length of the hash ...
.... I mean can I set the resulting hash to be always 4 characters
length whatever the length of the string being hashed?

And it will be unique?

Please, anyone?

Thanks,
Miguel
 
But I don't think I can control the length of the hash ...
... I mean can I set the resulting hash to be always 4 characters
length whatever the length of the string being hashed?

I have to ask - how many URLs are you planning on trying to store and
why the 4 character length limitation?

I think I agree with Dylan Parry's take on this problem. If you're going
to be storing the URL in a database table with a primary key, then the key
should always be unique.

It won't however be limited to 4 digits..
 
Resource = "article/show/c2f1bc05-756e-4096-b4dc-abc7b92e1214"
Alias = "4ikjh"

Personally I would just go for an auto incrementing integer on the table and
use that

site.com/meh/1
site.com/meh/2

etc

1: It's easier to read out over the phone
2: You avoid the possibility of accidental obscenitites

site.com/meh/2tits :-)
 
Back
Top