From 3fcd4b77c0a283946f60c66f82aa0e1c44139c10 Mon Sep 17 00:00:00 2001 From: gwern Date: Wed, 5 Jul 2017 04:07:50 -0400 Subject: Redirect: add checks for non-overlapping definitions --- lib/Hakyll/Web/Redirect.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/Hakyll/Web/Redirect.hs b/lib/Hakyll/Web/Redirect.hs index 4760cff..283d8bf 100644 --- a/lib/Hakyll/Web/Redirect.hs +++ b/lib/Hakyll/Web/Redirect.hs @@ -7,8 +7,9 @@ module Hakyll.Web.Redirect ) where import Control.Applicative ((<$>)) -import Control.Monad (forM_) +import Control.Monad (forM_, when) import Data.Binary (Binary (..)) +import Data.List (nub) import Hakyll.Core.Compiler import Hakyll.Core.Identifier import Hakyll.Core.Routes @@ -55,6 +56,10 @@ import Hakyll.Core.Writable (Writable (..)) -- See also . createRedirects :: [(Identifier, String)] -> Rules () createRedirects redirects = + do -- redirects are many-to-fewer; keys must be unique, and must point somewhere else: + let keys = map fst redirects + when (length keys /= length (nub keys)) $ error "Overlapping 301 redirects; check your mapping for redundant keys" + when (or $ map (\(r,t) -> toFilePath r == t) redirects) $ error "Self-redirect detected: at least one redirect's target points to itself." forM_ redirects $ \(ident, to) -> create [ident] $ do route idRoute @@ -81,6 +86,7 @@ instance Writable Redirect where redirectToHtml :: Redirect -> String redirectToHtml (Redirect working) = "" ++ + "" ++ "Permanent Redirect

The page has moved to: