From 887cd7c610ad4e24bf01faef630b946649a486c6 Mon Sep 17 00:00:00 2001 From: Unchained Date: Tue, 24 Mar 2026 07:36:55 +0200 Subject: [PATCH] feat: add 301 redirects for old Serbian URLs to preserve SEO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Redirect old Serbian URLs (without /sr/ prefix) to new /sr/ URLs: - / → /sr (301) - /products → /sr/products (301) - /about → /sr/about (301) - /contact → /sr/contact (301) - /checkout → /sr/checkout (301) English URLs (/en/*) remain unchanged. This preserves SEO value as Google treats 301 as permanent redirect passing ~90-99% PageRank. --- middleware.ts | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/middleware.ts b/middleware.ts index 5fcccb0..d09705a 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,9 +1,41 @@ import createMiddleware from "next-intl/middleware"; +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; import { routing } from "./src/i18n/routing"; -export default createMiddleware({ - ...routing, -}); +const oldSerbianPaths = ["", "products", "about", "contact", "checkout"]; + +export default function middleware(request: NextRequest) { + const pathname = request.nextUrl.pathname; + + const isOldSerbianPath = oldSerbianPaths.some((path) => { + if (path === "") { + return pathname === "/"; + } + return pathname === `/${path}` || pathname.startsWith(`/${path}/`); + }); + + const hasLocalePrefix = routing.locales.some( + (locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`) + ); + + if (isOldSerbianPath && !hasLocalePrefix) { + const newPathname = pathname === "/" + ? "/sr" + : `/sr${pathname}`; + + const url = request.nextUrl.clone(); + url.pathname = newPathname; + + return NextResponse.redirect(url, 301); + } + + const intlMiddleware = createMiddleware({ + ...routing, + }); + + return intlMiddleware(request); +} export const config = { matcher: [