diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -335,7 +335,7 @@ class hgweb(object):
req.url = req.env['SCRIPT_NAME']
if not req.url.endswith('/'):
req.url += '/'
- if 'REPO_NAME' in req.env:
+ if req.env.get('REPO_NAME'):
req.url += req.env['REPO_NAME'] + '/'
if 'PATH_INFO' in req.env:
diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -257,7 +257,7 @@ class hgwebdir(object):
repos = dict(self.repos)
- if not virtual or (virtual == 'index' and virtual not in repos):
+ if (not virtual or virtual == 'index') and virtual not in repos:
req.respond(HTTP_OK, ctype)
return self.makeindex(req, tmpl)
@@ -269,8 +269,17 @@ class hgwebdir(object):
req.respond(HTTP_OK, ctype)
return self.makeindex(req, tmpl, subdir)
- virtualrepo = virtual
- while virtualrepo:
+ def _virtualdirs():
+ # Check the full virtual path, each parent, and the root ('')
+ if virtual != '':
+ yield virtual
+
+ for p in util.finddirs(virtual):
+ yield p
+
+ yield ''
+
+ for virtualrepo in _virtualdirs():
real = repos.get(virtualrepo)
if real:
req.env['REPO_NAME'] = virtualrepo
@@ -284,11 +293,6 @@ class hgwebdir(object):
except error.RepoError as inst:
raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
- up = virtualrepo.rfind('/')
- if up < 0:
- break
- virtualrepo = virtualrepo[:up]
-
# browse subdirectories
subdir = virtual + '/'
if [r for r in repos if r.startswith(subdir)]:
diff --git a/tests/test-hgwebdir.t b/tests/test-hgwebdir.t
--- a/tests/test-hgwebdir.t
+++ b/tests/test-hgwebdir.t
@@ -1680,6 +1680,22 @@ Repos named 'index' take precedence over
$ killdaemons.py
+ $ cat > paths.conf << EOF
+ > [paths]
+ > / = $root/a
+ > EOF
+ $ hg serve -p $HGPORT1 -d --pid-file hg.pid --webdir-conf paths.conf
+ $ cat hg.pid >> $DAEMON_PIDS
+
+ $ hg id http://localhost:$HGPORT1
+ 71a89161f014
+
+ $ get-with-headers.py localhost:$HGPORT1 '' | grep 'index'
+
+ add index file
+
+ $ killdaemons.py
+
paths errors 1
$ cat error-paths-1.log