# Why does hyperref exhibit this behaviour on generated URLs? [duplicate]

I'm trying to create a class that, among other things, generates URLs from parameters passed to an environment. For some weird reason, if the URL string contains a /, hyperref tries to create a URL that points to a file named <URL>.pdf.

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}

\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
print   .bool_set:N = \l_myenvironment_print_bool,
}

\NewDocumentEnvironment { myenvironment } { m }
{
\keys_set:nn { mydocument / myenvironment } { #1 }
\minipage{\textwidth}

\tl_clear_new:N \l_item_url_tl

\tl_put_left:Nn \l_item_url_tl { https:// }

\href{\l_item_url_tl}{TeX.StackExchange} \\

\url{\l_item_url_tl} \\

}
{
\endminipage
}
\ExplSyntaxOff

\begin{document}

\begin{myenvironment}
{
}
Here is some content in my environment with a good link
\end{myenvironment}

\begin{myenvironment}
{
}
\end{myenvironment}

\end{document}


The above MWE exhibits the behaviour I'm describing: the first instance of the environment has a link to https://tex.stackexchange.com, but the second creates a link to https://tex.stackexchange.com/users/3351/robbie.pdf. Weirdly enough, it doesn't mangle the URL if I use \url, but I can't use that command in my document as the link text is also generated.

Why does it do this, and how do I make it use the raw URL value in the hyperlink target?

## marked as duplicate by TeXnician, user156344, Raaja, Stefan Pinnow, barbara beetonMar 25 at 18:09

• you are aware that you will run into problems with links contains special chars like % or ~? – Ulrike Fischer Mar 25 at 14:45
• I wasn't—thanks for the heads up—but the links I'm generating contain only alphanumeric characters and forward slashes, and the issue I'm describing only seems to occur when the URL is longer than just a domain name. – Robbie Mar 25 at 14:51

hyperref has an heuristic to decide the link type. It gets confused if the colon has catcode letter instead of other in your http:// text.

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}

\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
print   .bool_set:N = \l_myenvironment_print_bool,
}

\NewDocumentEnvironment { myenvironment } { m }
{
\keys_set:nn { mydocument / myenvironment } { #1 }
\minipage{\textwidth}

\tl_clear_new:N \l_item_url_tl
\tl_put_left:Nx \l_item_url_tl { http\c_colon_str// }

\href{\l_item_url_tl}{TeX.StackExchange} \\

\url{\l_item_url_tl} \\

}
{
\endminipage
}
\ExplSyntaxOff

\begin{document}

\begin{myenvironment}
{
}
Here is some content in my environment with a good link
\end{myenvironment}

\begin{myenvironment}
{