What does it mean, that an URL is valid

Many developers are mixing the idea of whether an URL is valid or “openable”, that is — any browser is able to open it. Having an URL that matches second condition doesn’t actually mean, that such particular URL is valid. This article tries to deal with this little misunderstanding.

If an URL can be opened in any browser, then it doesn’t mean, that it is valid. It only means, that browser will do as much as it can to convert “user stupidities” into “openable” URL. I stumbled upon this, when looking for URL validator for Javascript. There are many (dozens) of answers about it here on SO and there are hundreds of comments / examples of URLs, that can be opened in browser, but are claimed to be invalid.

There’s a GitHub Gist that matches this situation perfectly. Out of given six example URLs:

  • www.asdf.com,
  • example.org,
  • /peach.kingdom,
  • x.something.co.uk,
  • http:myname.com,
  • https://www.sdf.org,

five or six can be opened by certain web browsers (try to select each and pick Go to... from context menu). But, only last one is actually a valid one.

The general rule of thumb here is: If you want to check, if an URL is valid, then using method described in Dan Grossman’s answer (the one, that uses filter_var):

if (filter_var($url, FILTER_VALIDATE_URL) !== false) ...

or any similar is the right choice. FILTER_VALIDATE_URL validates URLs according to RFC 2396 and makes you sure, that your URL is valid.

If, on contrary, you want to check if URL is “openable” then the only way you’re left with, is to actually open it with any PHP url opener or wrapper method and check for returned result. There is absolutely no URL validator that will return true for any URL that any browser is able to open / parse / “understand”.

Leave a Reply