URLs with unique random tokens in Node.js

For my project I'd like users to be redirected to their own unique url when they arrive to the website.

e.g. user goes to mydomain.com they get redirected to mydomain.com/xyz123

I have this working. However I have 2 questions:

  1. How many characters should the random token ideally have? (is there a formula for calculating total amount of tokens possible?)
  2. How do I stop bots constantly hitting the site and taking up these tokens?

Thanks for your help.

  1. This depends on how many visitors (users?) you expect to have. If you stick to just Latin letters and numerals, and are case sensitive, that gives you 62 unique possibilities per character (26 * 2 + 10), so 62^n where n is the number of characters in your URL. 62^5 is just over 916 million which should last a while.
  2. This is much harder. Basically you would need to look at the user agent string and determine whether the visitor was a bot. If they are, don't bother redirecting them. See: How to recognize bots with php?