on master help sign in
add redirectWWW

I put it in Vhost. probably should rename the module now

commitb3f622a1d3e0f239ae224e81d2fcee51bb56277f authorGreg Weber <greg@gregweber.info> dateSun 26 Feb 2012, 07:05 (15 years ago) parentb7fc5ee
1 file changed+15−1
wai-extra/Network/Wai/Middleware/Vhost.hs+15−1
wai-extra/Network/Wai/Middleware/Vhost.hs+15−1
@@ -1,9 +1,23 @@
1−module Network.Wai.Middleware.Vhost (vhost) where
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Wai.Middleware.Vhost (vhost, redirectWWW) where
23
34import Network.Wai
45
6+import Network.HTTP.Types as H
7+import qualified Data.Text.Encoding as TE
8+import Data.Text (Text)
9+import qualified Data.ByteString as BS
10+
511vhost :: [(Request -> Bool, Application)] -> Application -> Application
612vhost vhosts def req =
713 case filter (\(b, _) -> b req) vhosts of
814 [] -> def req
915 (_, app):_ -> app req
16+
17+redirectWWW :: Text -> Application -> Application -- W.MiddleWare
18+redirectWWW home app req =
19+ if BS.isPrefixOf "www" $ serverName req
20+ then return $ responseLBS H.status301
21+ [ ("Content-Type", "text/plain") , ("Location", TE.encodeUtf8 home) ] "Redirect"
22+ else app req
23+