I am working on a project that involves a url “forwarder” (like bit.ly or tinyurl.com, but we don’t really need it to be short).
For that, I need to “generate” alphanumeric strings (I explicitly want alphanumeric) to map to each url. One of the options would be generate a random string and store it somewhere. However, I’d like to avoid using a database since we don’t use any in our app. I want to actually “encode” the url so that it can be decoded later.
Any tips on how to do that?
Can’t be done. An arbitrary URL contains many characters — let’s say 100. A shortened URL contains maybe 5. You can’t use 5 characters to reconstruct 100 without a lookup table of some kind; there’s simply not enough information available to do it.
EDIT 1: Well, if you don’t actually need a URL shortener (then why did you write that?), there are plenty of options. I’d go for plain Base64 encoding, perhaps after a pass through zlib or another compressor (that might make URLs longer; you’ll have to measure if it helps or not).
EDIT 2: Standard Base64 does use three non-alphanumeric characters:
+,/, and-. If these are unacceptable, you have a couple of options:Modified Base64. Wikipedia suggests “modified Base64 for URL”, which drops all
=and replaces+and/with-and_respectively. But those still aren’t alphanumeric, which doesn’t help you.Some ad-hoc scheme, like Base32 or Base36. This is really easy to implement if you know how Base64 is done (see above link). (Edit 3: I guess Base32 is actually standardized. Looks like RFC 4648 Base32 with
8padding instead of=padding would work just fine for you).Some semi-standard approach. There are plenty of possibilities. Unfortunately, most of them rely on a couple of special non-alphanumeric characters, simply because by using as few as one or two more characters you can get far superior performance. Take a look at Binary-to-text encoding for a better survey than I can give.