2

I want to implement a URL shortener, with the need that people can customize the shorten url code.

In a simple url shortener, people get a messy code for a long url. But in some scenarios, one may want to customize or specify the short url to be more readable and self-explaining. For example, url.com/jimhome rather than url.com/D3aK1 . This makes two parameters: the long url and the specifed short url. The system need to check whether the short url is used of course.

I have researched How to code a URL shortener? and find it good to use.

What's on my mind is: make 3 fields in DB, id, long url, short url. With a regular request without customization, do as the post does and store the generated short url.With a request with customization, first check existence, if not exist, insert the long url and short url, but this breaks the accordence between ID and short url. Is there a more elegant way?

I'm not native English speaker, if you got confused, please leave a comment. Thanks in advance.

Community
  • 1
  • 1
matrix
  • 349
  • 3
  • 12
  • 2
    "but this breaks the accordence between ID and short url" - what do you mean by that ? – Nir Alfasi May 25 '15 at 04:53
  • @alfasin In the linked Post Answer I referenced, the short url is generated from the ID of the row in DB, which means that short url is just another representation of ID. In fact, in that approach, storing short url is not necessary. The approach I proposed involves many "checking", I'm looking for a better way. – matrix May 25 '15 at 06:15
  • If you're looking for "meaningful" urls (that are set by the client - the ID becomes meaningless. You should decide which one is it: meaningful URL or meaningless (different implementation). – Nir Alfasi May 25 '15 at 17:06

1 Answers1

0

If I understand correctly, the problem you are trying to avoid is that the ID for each url is assigned by mysql auto increment, but a custom url would require a specific ID.

The simplest way to avoid that would be to pick the IDs differently. For example you could take the current unix time-stamp and use that as your id.

The two potential issues would be the same ID generated (two concurrent users) and the URL being longer than you want it to be. This could be addressed by decreasing the number in some arbitrary way and retrying if there is a conflict.

Example:

//create a unique ID
$ID = time()/100000;

while(tableContains($ID))
     $ID++;

insertRow($ID, idToShortURL($ID), $longURL);

This psuedo code is just dividing to make it smaller. If then will increment the ID until it is unique before adding it.

Custom IDs would something like:

$ID = urlToID($longURL);//using the method described in your linked question

if(tableContains($ID)
     echo 'Error: the custom url is already in use';
else
     insertRow($ID, idToShortURL($ID), $longURL);

I hope that's not too vague and this is just the first thing I thought of. There are a million legitimate ways to accomplish the goal. As long as you are picking the ids in some way that order is independent of the assigned ID you will avoid the issue in question.

  • Well yes, he can do that, but he specifically asked about "meaningful" urls that are set by the customer: For example, `url.com/jimhome` rather than `url.com/D3aK1` – Nir Alfasi May 25 '15 at 17:05
  • How to handle the use case that if there is custom URL added like url.com/stacks that same with the hashed increment ID of ID 123456 (url.com/stacks), and the increment ID is not reach 123456 yet? – Yosua Lijanto Binar Aug 31 '16 at 10:51