$$\int{\frac{1}{a^2-x^2}}dx\tag{1}$$
the range of $x$ is $\mathbb{R}-\{-a,a\}$ while the range of
$a\sin(\theta)$ is $[-a,a]$
how can $a\sin\theta$ be substituted for $x$ in $(1)$ when the range
of $x$ and $a\sin\theta$ are not the same?
On the integration domain $(-a,a)$ (let's call this interval $A$), the substitution $x=a\sin\theta$ with range $[-a,a]$ (let's call this interval $B$) is valid because $A\subseteq B.$
On the separate integration domains $(-\infty,-a)$ and $(a, \infty)$ (let's call these intervals $C$ and $D,$ respectively), the substitution $x=a\sec\theta$ with range $[-\infty,-a]\cup[a,\infty]$ (let's call this set $E$) is valid because $C\subseteq E$ and $D\subseteq E.$
(Alternatively, use the hyperbolic substitutions $x=-a\cosh\theta$ and $x=a\cosh\theta$ for integration domains $C$ and $D,$ respectively.)
But this integral is easiest found using partial fractions, without any substitution.