diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 03f95826fcc14992865a9eabf5949efc21622c44..5de6fad8a64422d46649eada14048196c96a25fd 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -38,7 +38,7 @@ check for dependencies installed:
 build documentation:
   stage: test
   script:
-    - make -C doc realclean; make -C doc html SPHINXOPTS='-A website_deploy=True -n -W' REFNAME="${CI_COMMIT_TAG:-$CI_COMMIT_SHA}" SOURCE_URL="$CI_PROJECT_URL"/blob
+    - make -C doc realclean; make -C doc html SPHINXOPTS='-A website_deploy=True -n -W' SOURCE_LINK_TEMPLATE="$CI_PROJECT_URL"/blob/$$r/$$f
   artifacts:
     paths:
       - doc/build/html/
diff --git a/RELEASE.rst b/RELEASE.rst
index 12074d7ae9ef553a01697ccc0e6b50040f5bc75f..468b8fcd1d96e0495bded930934579d300a014fc 100644
--- a/RELEASE.rst
+++ b/RELEASE.rst
@@ -112,7 +112,7 @@ Building the documentation requires 'sphinx' and a Latex installation.
 First build the HTML and PDF documentation::
 
     ./setup.py build
-    make -C doc realclean html latex SPHINXOPTS='-A website_deploy=True -n -W' REFNAME="<version>"
+    make -C doc realclean html latex SPHINXOPTS='-A website_deploy=True -n -W'
     cd doc/build/latex
     make all-pdf
 
diff --git a/doc/source/conf.py b/doc/source/conf.py
index d2688e8701a0dcc0c6e191fd1f1d08211be5b648..90c1cd7faf1abb86a64600dde5b9d783a73d32b2 100644
--- a/doc/source/conf.py
+++ b/doc/source/conf.py
@@ -11,8 +11,9 @@
 # All configuration values have a default; values that are commented out
 # serve to show the default.
 
-import sys, os
-
+import sys
+import os
+import string
 from distutils.util import get_platform
 sys.path.insert(0, "../../build/lib.{0}-{1}.{2}".format(
         get_platform(), *sys.version_info[:2]))
@@ -298,8 +299,18 @@ def linkcode_resolve(domain, info):
         filename = 'kwant/%s#L%d-%d' % find_source()
     except Exception:
         filename = info['module'].replace('.', '/') + '.py'
-    source_branch = os.environ.get('REFNAME', 'master')
-    url = os.environ.get('SOURCE_URL',
-                         "https://gitlab.kwant-project.org/kwant/kwant/blob")
 
-    return "{}/{}/{}".format(url, source_branch, filename)
+    # The following relies on the documented format of kwant.__version__.
+    rel = release.rstrip('.dirty')
+    if '.dev' in rel:
+        try:
+            refname = rel[rel.index('+g') + 2:]
+        except ValueError:
+            return
+    else:
+        refname = 'v' + rel
+
+    templ = os.environ.get(
+        "SOURCE_LINK_TEMPLATE",
+        "https://gitlab.kwant-project.org/kwant/kwant/blob/$r/$f")
+    return string.Template(templ).safe_substitute(r=refname, f=filename)