Udi
Phoenix route path with suffix (can route be made to look like static .html file?)
I have routes that look like this:
get("/post/:slug_and_id", PostController, :show)
get("/post/:slug_and_id/comment/:comment_id", PostController, :comment)
Instead of getting urls like
/post/my-post-slug-123
I’d instead want them to look like this
/post/my-post-slug-123.html
It feels like I should just be able to do this, but it doesn’t work:
get("/post/:slug_and_id.html", PostController, :show)
get("/post/:slug_and_id/comment/:comment_id.html", PostController, :comment)
Is this possible? Perhaps via some custom Plug? Has anyone ever done something like this in their phoenix app?
Thanks!!
Udi
Most Liked
Udi
Thanks for the suggestions!
I think this approach will work for me, but I still need to try it out:
get("/post/*slug_and_id", PostController, :show, slug_and_id: ~r/[^\/]+\.html$/)
Then I have to change my parsing of slug_and_id to take out the .html. Then I’ll also have to adjust all places where I generate links using the path helper to manually append the suffix but that’s not too bad.
As to the why… I have an existing site with 100k+ posts on it that I want to convert into a static site (I no longer allow contributions, so it’s effectively static) to save on costs. This would be a step in that direction and would make it easier for me to use wget or some other method to download a fully static version of the site.
100phlecs
Here’s a thread with roughly the same question. The answer is to modify params with a plug:
Though the above is a little old, maybe things are different today.
A guess would be porting over a website which previously had the convention of .html at the end of a route ![]()
arcanemachine
Maybe this library would be helpful?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









