What are URL segments?

URL segments are the parts of a URL or path delimited by slashes. So if you had the path /path/to/page/ then "path", "to", and "page" would each be a URL segment. But ProcessWire actually uses the term "URL segments" to refer to the extra parts of that URL/path that did not resolve to a page.

Lets assume that the path /products/tools/hammer/ resolved to an actual page in our site when accessed at that URL. But lets say that we accessed the URL /products/tools/hammer/photos/ and the /photos/ portion at the end does not resolve to a page. Because "photos" did not resolve to a page, ProcessWire considers this a URL segment for the "hammer" page. If URL segments are enabled for the page's template, then the "hammer" page will displayed, despite the fact that we had the "photos" segment at the end. Because it is the first URL segment that didn't resolve to a page, it is considered URL segment #1.

If there was yet another segment appended to it, like /products/tools/hammer/photos/grip/ then "photos" would be URL segment #1 and "grip" would be URL segment #2. The "hammer" page would still be displayed, but its template file could respond to the different URL segments however it wanted. In this case, the logic in the template file might look for "photos" in URL segment #1 and display a photo gallery when present.

As you might gather from this example, URL segments enable your page's template file to become a URL router or controller to act upon different URLs sent to it.

Next: How and where to use them »


  1. What are URL segments?
  2. How and where to use them
  3. Best practices
  4. Page numbers and URL segments

Comments

  • Chris

    Chris 2 years ago 22

    These url segments, although cleaner, seem just like a query string. how do they differ?

    • ryan

      ryan 2 years ago 142

      Some reasons to use URL segments rather than query strings:

      1. Pages rendered from URL segments can be cached, whereas pages rendered as the result of a query string cannot.

      2. URL segments are search engine friendly, whereas query strings are not necessarily.

      3. URL segments are sanitized automatically, since they have to follow PW page name format, whereas query strings are not.

      4. URL segments are nicer to look at, easier to read, and easier to type.

      But should you replace your use of query strings with URL segments? Nope. I would always still use query strings in these situations:

      1. The potential values you accept are unlimited. You would not want such values to benefit from caching (per item 1 above) since a cache with no limits can fill up a drive.

      2. You are in an environment where things like search friendliness and readability aren't really a factor. For instance web applications with a logged in user is likely to derive more benefit from using query strings.

      3. You need true "key=value" relationships with variables. Keep in mind URL segments are just the 'value' part. You might translate /key1/value1/key2/value2, etc., but that's more work and things to keep track of than a nice simple key=value query string.

      4. One of the biggest: if you need to allow values outside PW page name format. Keep in mind PW page names are limited to this set of characters: -_.a-z0-9. So you aren't going to want to use URL segments for a text search engine for example.

      5. The quantity of variables in the URL is unknown. URL segments are naturally limited to just a few. Whereas you could have any number of variables present in a query string.

      • Chris Falkenstein

        Chris Falkenstein 2 years ago 43

        Ryan, thanks for the informative response. and thank you for your generous offering here, pw. The more I use it the more I like it. Keep up the excellent work.

  • Alan

    Alan 2 years ago 11

    Coming from WordPress it seems to me this Would be an optimal way to use category/tags. But what can be a better use for this? =)

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.