From 26dd1591f6a8a929b9c93df27a59b5402c9c7162 Mon Sep 17 00:00:00 2001
From: Moritz Marquardt <git@momar.de>
Date: Thu, 2 Dec 2021 00:00:00 +0100
Subject: [PATCH] Add www redirect for main domain

---
 handler.go | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/handler.go b/handler.go
index f563b14..44865ac 100644
--- a/handler.go
+++ b/handler.go
@@ -180,6 +180,12 @@ func handler(ctx *fasthttp.RequestCtx) {
 		targetRepo = pathElements[0]
 		targetPath = strings.Trim(strings.Join(pathElements[1:], "/"), "/")
 
+		if targetOwner == "www" {
+			// www.codeberg.page redirects to codeberg.page
+			ctx.Redirect("https://" + string(MainDomainSuffix[1:]) + string(ctx.Path()), fasthttp.StatusPermanentRedirect)
+			return
+		}
+
 		// Check if the first directory is a repo with the second directory as a branch
 		// example.codeberg.page/myrepo/@main/index.html
 		if len(pathElements) > 1 && strings.HasPrefix(pathElements[1], "@") {