I've this problem where an Exercise environment automatically generates a reference to its Solution, and while the page displayed is correct, clicking on it leads to the Solution's chapter (if any) or does nothing if there's no chapter.

Minimal example:

\documentclass[english,a4paper,12pt]{book}

\usepackage{amsthm}
\Newassociation{sol}{Solution}{ans}
\usepackage{hyperref}

\newtheorem*{exercise*}{Exercise}
\theoremstyle{plain}
\newenvironment{Exercise*}{\noindent\begin{exercise*}}{\end{exercise*}}
\newtheorem{innerexercise}{Exercise}
\newenvironment{exercise}[1]
{\begin{innerexercise}\label{#1} (p.~\pageref{sol:#1}) \renewcommand{\Currentlabel}{#1}}
{\end{innerexercise}}
\newenvironment{Exercise}[1]{\noindent\begin{exercise}{#1}}{\end{exercise}}

\newtheorem*{solution*}{Solution}
\newenvironment{Solution*}{\noindent\begin{solution*}}{\end{solution*}}
\newtheorem*{innersolution}{Solution{} \exerciseref}
\newenvironment{solution}[1]
{\def\exerciseref{\ref{#1}}%
\innersolution\label{sol:#1} (p.~\pageref{#1})}
{\endinnersolution}
\renewenvironment{Solution}[1]{\noindent\begin{solution}{#1}}{\end{solution}}

\begin{document}
\Opensolutionfile{ans}[solutions]

\begin{Exercise}{firstexercise}
First exercise here.
\begin{sol} % label is auto generated: sol:firstexercise
First solution here.

\newpage % forcing break between one solution and another so to display two solutions in two different pages
\end{sol}

\end{Exercise}

\begin{Exercise}{secondexercise}
Second exercise here.
\begin{sol}
Second solution here.
\end{sol}

\end{Exercise}

\Closesolutionfile{ans}
\input{solutions}
\end{document}


This code creates two environments, Exercise and Solution (which simply hold the two environments that do the actual work, "exercise" and "solution"; this extra layer is for code clarity in case one wants to add some graphical embellishments).

"Exercise" takes one argument, which then becomes its label. Inside an Exercise one has to define a "sol" environment which hosts its parent exercise's solution.

This code automatically generates references from Exercise A to its solution and vice versa.

Solutions aren't displayed inside their exercises: they are generated and grouped in a solution file (here, "solutions").

This code was generated thanks to community help as seen in my previous question here: Cross-reference two numbered environments

So, here's the issue. This minimal example generates two exercises and two solutions:

The page references are correct, so one'd assume they'd point to the correct pages too. That is not the case: they do nothing. What's going on?

When setting a \label, hyperref uses the last mark as the hyper-target. If you don't specify this in the correct way, your links might end up where they shouldn't go.
You can place a \phantomsection inside your solution environment. This re-establishes the hyper-target location:
\newenvironment{solution}[1]