Skip to content

Instantly share code, notes, and snippets.

@mithereal
Last active June 23, 2017 18:35
Show Gist options
  • Save mithereal/49da2fd02f32c068b435087fe1e56585 to your computer and use it in GitHub Desktop.
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
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