diff --git a/hooks/site_urls.py b/hooks/site_urls.py
index df01fab45cfae0b59a6ab8a5f2f0f1dee4e3d76f..af5599790fd156db96ef019b07692cf0ed8815ce 100644
--- a/hooks/site_urls.py
+++ b/hooks/site_urls.py
@@ -54,15 +54,17 @@ def on_page_content(html, page, config, files):
     if not path.endswith("/"):
         path += "/"
 
-    if not use_directory_urls:
-        path += "index.html"
-
-    def _replace(match):
+    def _replace(match, use_directory_urls: bool = use_directory_urls):
         param = match.group(1)
         url = match.group(2)
         if url.startswith("/"):
             url = url[1:]
 
+        if not use_directory_urls:
+            if not url.endswith("/"):
+                url += "/"
+            url += "index.html"
+
         log.info(f"REPLACING site:{match.group(2)} with {path}{url}")
         return f'{param}="{path}{url}"'