A suggestion is to request the URL. If it detects redirect, it may be a "shortener", but it may also not be and would then be preventing legitimate Urls.
From this point, compare with the number of characters. If it is less than X it increases the probability of being a shortener and discards larger Urls that redirect but are not shorteners.
It looks good so far, but in practice, it doesn’t work.
Let’s go to a real example:
tinyurl.com <- is a URL shortener
globe with. <- is smaller than tinyurl.com but is not a shortener.
R7.with <- is even smaller..
So the logic of comparing the size of the URL or domain is not valid.
Perhaps you should ask yourself, what is the purpose of detecting if it is a URL shortener?
If it’s because they redirect a URL, then it could just validate the redirect, regardless of whether it’s a shortener or not.
Do you have a list of shortened url’s that you won’t accept? If not, the first step is to list all you find so you can create an efficient regular expression.
– LF Ziron
You can also create a rule: "domain with at least x characters" which are in the format "domain/key" = lock. You can look for other criteria, observing the shorteners that exist, to avoid "false positive" and have a manageable and efficient validation.
– mau humor
Another way is to send a request to the posted URL, if it is redirected, you can get the real URL, or block, because it is not a direct link.
– mau humor
I agree with the replies of @user5978 . The big problem is that maybe the site itself that the user posts the link does a redirect. In this case it could be checked whether the redirect was made to the same domain, but it would still be risky. Let’s say I entered the url: http://foo.com/bar that redirects users from Europe to http://foo.com.eu/bar, the system would block this type of url, and this is a type of redirect that even google does to handle the access of users from different countries
– LF Ziron
Yes, if you opt for a generic solution, the system will have to have a certain "ineligibility" to deal with false positives. That’s the "price" for not having to constantly update the list of shorteners, hehe.
– mau humor
This site knows whether or not it is a "shortened" URL. http://www.checkshorturl.com/expand.php. This is not an API, but your script can submit the URL, and analyze the return.
– mau humor