\( \LaTeX \) is fantastic, but sometimes it does strange things, like breaking \urls only at certain points. Using tricks like this glorious \UrlBreaks hack, one can force URLs to break at any point, but for some reason, this does not work in environments like itemize, enumerate and adjustwidth, so doesn't work in BibLaTeX bibliographies.

To overcome this problem, one can manually insert \allowbreaks, but that seems a little s\allowbreaki\allowbreakl\allowbreakl\allowbreaky.

The solution: use Lua to do the hard work.

function betterurl(text)
label = text:gsub(".", "%1\\allowbreak{}")

label = label:gsub("~", "\\textasciitilde")
label = label:gsub("&", "\\&")

-- insert other URL symbols here

url = text

tex.print("\\href{" .. url .. "}{\\texttt{" .. label .. "}}")

LaTeX preamble