Connection lost
Reconnecting. If you just saved something, check that it saved before trying again.
Server error
Reconnecting to the server. If you just saved something, check that it saved before trying again.
Redirecting known trailing-slash aliases in Phoenix
almirsarajcic
I was checking Who Does AI? in Google Search Console to see whether its public pages were being indexed. The Page indexing report was still processing, so I asked the agent to inspect the sitemap URLs individually. All eight were already indexed, including /hire.
We also checked /hire/. It returned HTTP 200 and displayed the same hiring form, but its HTML had the generic membership title, noindex, nofollow, and no canonical tag.
The router accepted both paths. Our metadata lookup used the exact conn.request_path, with an entry for /hire. Adding the slash missed that entry and fell back to the metadata for membership pages.
Both addresses represented one page, so we chose to redirect /hire/ to /hire. Our routes and metadata already used paths without a trailing slash. Either convention works; keeping this one avoided changing the established public URLs. The same mismatch affected six other pages, so we added this endpoint Plug:
defmodule WhoDoesAiWeb.PublicTrailingSlash do
import Plug.Conn
@aliases ~w(/hire/ /work/ /about/ /contact/ /privacy/ /terms/ /agents/)
def init(opts), do: opts
def call(%{method: method, request_path: path} = conn, _opts)
when method in ["GET", "HEAD"] and path in @aliases do
target = String.trim_trailing(path, "/")
query = if conn.query_string == "", do: "", else: "?" <> conn.query_string
conn
|> put_resp_header("location", target <> query)
|> send_resp(301, "")
|> halt()
end
def call(conn, _opts), do: conn
end
The Plug sends a 301 to the matching no-slash URL and halts the pipeline before the request reaches the router. Register it with plug WhoDoesAiWeb.PublicTrailingSlash before plug Plug.Parsers in endpoint.ex.
For Google, /hire and /hire/ are separate URLs. The accidental noindex told it to exclude the slash version from search results. It did not point to /hire as the preferred copy. A permanent redirect sends visitors to that page and gives Google a strong signal to use it as canonical.
Changing the sitemap would only change the addresses we ask search engines to discover. It would leave /hire/ serving the wrong metadata whenever someone requested it. We could instead make the metadata lookup recognize both forms and give both a canonical tag pointing to /hire, but the redirect also takes visitors to that address.
We kept the alias list explicit and limited redirects to GET/HEAD. Write methods and unknown paths continue through the endpoint, so this doesn’t turn every slash-ended request into a redirect.
The query string needed care too. Decoding it into a map would collapse repeated keys. Appending conn.query_string directly keeps /hire/?utm_source=a+b&tag=x&tag=y as /hire?utm_source=a+b&tag=x&tag=y, including both tag values and the original escaping.
The follow-up checks confirmed 301 redirects for all seven aliases, with the query string preserved. This fixes the inconsistent responses; it doesn’t establish a ranking improvement.
There’s also trailing_slash_plug. Its v0.1.3 implementation preserves the raw query string, but applies to all methods and slash-ended paths in scope. It sets the status and Location header without sending or halting the response, so it isn’t a drop-in equivalent to the Plug above.