Last active
June 23, 2017 18:35
-
-
Save mithereal/49da2fd02f32c068b435087fe1e56585 to your computer and use it in GitHub Desktop.
route bots to a custom route so we can seed them with seo friendly content via plug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Site.Plug.Bot.Seo do | |
import Plug.Conn | |
@doc """ | |
This plug will route bots to a custom route so we can seed them with seo friendly content | |
requires (add to mix): {:ua_inspector, "~> 0.13" } | |
""" | |
@url "/to_url" | |
def init(options), do: options | |
def call(conn, _options) do | |
ua = conn.req_params["user-agent"] | |
case UAInspector.bot? ua do | |
_ -> do_redirect(conn, @url) | |
end | |
end | |
defp do_redirect(conn, nil), do: conn | |
defp do_redirect(conn, to) do | |
conn | |
|> Phoenix.Controller.redirect(to: to) | |
|> halt | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment