# Parsing URLs with elm/url

**URL:** <https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373>\
**Category:** Show and Tell\
**Created:** [May 18, 2021, 6:58pm UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373 "2021-05-18T18:58:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexkorban](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alexkorban/32/3242_2.png) [@alexkorban](https://discourse.elm-lang.org/u/alexkorban)\
**Post date:** [May 18, 2021, 6:58pm UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/1 "2021-05-18T18:58:24Z")

</div>

I wrote a fairly extensive [guide to parsing URLs](https://korban.net/posts/elm/2021-05-18-parse-urls-elm-url/) in Elm: routing, handling query strings, working with hash fragments, reusing parsers and dealing with path segment prefixes.

Probably most useful to those who are new to Elm.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [May 19, 2021, 11:14am UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/2 "2021-05-19T11:14:20Z")

</div>

That doesn’t seem to handle the case where path prefix is e.g. `/foo` but you have link to `/bar`.

---

<div class="post-metadata">

**Author:** ![alexkorban](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alexkorban/32/3242_2.png) [@alexkorban](https://discourse.elm-lang.org/u/alexkorban)\
**Post date:** [May 19, 2021, 9:45pm UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/3 "2021-05-19T21:45:06Z")

</div>

Do you mean a link to, say, `example.com/elm` when my app is served from `example.com/elm/catalog/`? That would be an external URL, so in my situation I just use a full URL like `example.com/elm` in those links.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [May 20, 2021, 8:21am UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/4 "2021-05-20T08:21:14Z")

</div>

Yes. So is full URL always sent to `onUrlRequest` handler even when within current domain?

(Link to `/elm` would be sent to `onUrlChange` and would require custom code to be handled correctly, which your example doesn’t have.)

---

<div class="post-metadata">

**Author:** ![alexkorban](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/alexkorban/32/3242_2.png) [@alexkorban](https://discourse.elm-lang.org/u/alexkorban)\
**Post date:** [May 20, 2021, 11:55pm UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/5 "2021-05-20T23:55:04Z")

</div>

You could do something like

```auto
case urlRequest of
    Internal url ->
        if not <| String.startsWith ("/" ++ model.urlPrefix) <| url.path then
            ( model, Nav.load <| Url.toString url )

        else
            ( model, Nav.pushUrl model.navKey <| Url.toString url )

    External url ->
        ( model, Nav.load url )

```

This check would fail if the prefix is empty, but the other problem is that when you’re just running the app on localhost by itself, `/elm` is not a valid link (there is no “parent” to link to). I’m just not sure this is a particularly useful solution, that’s why I didn’t include it.

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [May 21, 2021, 10:09am UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/6 "2021-05-21T10:09:02Z")

</div>

> [@alexkorban](#):
>
> … the other problem is that when you’re just running the app on localhost by itself, `/elm` is not a valid link (there is no “parent” to link to) …

I don’t understand - `localhost` is as valid host as anything else, there is no difference in running Elm under `localhost` or `example.com`

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 31, 2021, 10:09am UTC](https://discourse.elm-lang.org/t/parsing-urls-with-elm-url/7373/7 "2021-05-31T10:09:48Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
