I have to make a system which can give us short version of URL and a way to track clicks.
This gist is about how I solved the first part of problem: as short as possible URL.
First things first. If we talk about system which handle URLs then we should have a table in database.
Example table of URLs:
ID | URL |
---|---|
1 | https://example.com/ |
2 | https://reddit.com/ |
… | |
174579021 | https://random.cat/ |
My solution is to convert urls ID into base-n number system. This way allows to leave the database table as it is.
For example if we convert last url ID 174579021 to base 16 we will get A67DD4D. But if we convert the same ID to base 64 with custom chars (from javascript example below) we will get 9OxpC.
Now I can implement simple action /:convertedID
, convert ID back to decimal and search the ID through primary key.
Benefits:
- DB table remains the same
- User sees pseudo-random (because of converted primary key) short link
- No one cannot search through other links until they know char set
Channel | Author |
---|---|