The space is there in the .bbl file already
\begin{APACrefDOI} \doi{10.1348/014466604X18523} \end{APACrefDOI}
So you could edit the .bst file not to add a space, or we can add \ignorespaces to the APACrefDOI environment.
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{hyperref}
\usepackage{apacite}% load apacite after hyperref, see §8.2 of the apacite manual
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{appleby,
author = {Humphrey Appleby},
title = {On the Importance of the Civil Service},
year = {1980},
doi = {10.1348/014466604X18523},
}
\end{filecontents}
\newcommand*{\doi}[1]{\href{https://doi.org/#1}{#1}}
\makeatletter
\renewenvironment{APACrefDOI}{%
\global\let\old@doi\doi
\if@doi
\doiprefix
\else
\global\let\doi\@gobble
\fi
\ignorespaces
}{\global\let\doi\old@doi }
\makeatother
\begin{document}
\cite{appleby}
\bibliographystyle{apacite}
\bibliography{\jobname}
\end{document}

If you prefer to modify the .bst file, you need to follow the steps below.
- Locate
apacite.bst on your machine for example by typing kpsewhich apacite.bst.
- Copy the file to a place where LaTeX can find it (https://texfaq.org/FAQ-inst-wlcf), the directory of your document will do just fine, and rename it to
apacite-nspdoi.bst, say. Note that the license of apacite.bst requires you to change the name of the file if you modify it.
- Open
apacite-nspdoi.bst and insert a header with the new file name and the current date
Find FUNCTION {begin.end.doi.env} and replace the entire function with
FUNCTION {begin.end.doi.env}
{ "\begin{APACrefDOI}"% removed trailing space here
" \end{APACrefDOI} "
}
Where the only change to the original is that you delete the last space in "\begin{APACrefDOI} " to make it "\begin{APACrefDOI}".
Save the file and use \bibliography{apacite-nspdoi} in your document.
Alternatively you can get apacite-nspdoi.bst from https://gist.github.com/moewew/520dbd08e1a7a3129a8838d5e6be34a3
\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{hyperref}
\usepackage{apacite}% load apacite after hyperref, see §8.2 of the apacite manual
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{appleby,
author = {Humphrey Appleby},
title = {On the Importance of the Civil Service},
year = {1980},
doi = {10.1348/014466604X18523},
}
\end{filecontents}
\newcommand*{\doi}[1]{\href{https://doi.org/#1}{#1}}
\begin{document}
\cite{appleby}
\bibliographystyle{apacite-nspdoi}
\bibliography{\jobname}
\end{document}
The output is as above.