Skip to content

Commit

Permalink
Using redirects to make pretty API URLs
Browse files Browse the repository at this point in the history
We want our API to be accessible on the `/api/pokemon` route, so in `netlify.toml` we can add a rewrite, which is different than a redirect.

A redirect will change the url in the browser, while a rewrite will not. So the user will still see `/api/pokemon/:slug` instead of seeing the URL change to `/.netlify/functions/pokemon`. We specifically need to use the `200` status code to get a rewrite.

We can put the rewrite in the `netlify.toml` under the `redirects` key. Netlify allows us to specify a `from` url, a `to` url, the status code, and more if we wanted

```bash
[[redirects]]
from = "/api/pokemon/:slug"
to = "/.netlify/functions/pokemon"
status = 200
```

Now redeploy and we can use our new URL
  • Loading branch information
ChristopherBiscardi committed Oct 24, 2021
1 parent 33babf4 commit df01e51
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion netlify.toml
Expand Up @@ -7,4 +7,9 @@ mkdir functions &&
cargo build --release --bin pokemon-api &&
cp target/release/pokemon-api functions/
"""
publish = "crates"
publish = "crates"

[[redirects]]
from = "/api/pokemon/:slug"
to = "/.netlify/functions/pokemon"
status = 200

0 comments on commit df01e51

Please sign in to comment.