<pre><code>{% if craft.app.request.segments|length &gt; 1 %}
  {% set secondToLast = craft.app.request.segments|slice(-2, 1)|first %}
{% else %}
  no second to last
{% endif %}
{{ secondToLast }}
</code></pre>

<p><a href="https://docs.craftcms.com/api/v3/craft-web-request.html#method-getsegments" rel="nofollow noreferrer">getSegments()</a> returns an array of segments</p>

<p>in line 1: check if there is a second to last item</p>

<p><a href="https://www.php.net/manual/en/function.array-slice.php" rel="nofollow noreferrer">slice</a>(-2, 1): -2 target second to last item and you only that one so second parameter is 1</p>

<p><code>|first</code> is because slice returns an array</p>

<hr>

<p>this solution need some tests and some consideration
for example <a href="https://docs.craftcms.com/api/v3/craft-web-request.html#method-getsegments" rel="nofollow noreferrer">Craft docs</a> says:</p>

<blockquote>
  <p>Note that the segments will not include the control panel trigger if
  it’s a control panel request, or the page trigger or page number if
  it’s a paginated request.</p>
</blockquote>

CC BY-SA 4.0
2020-04-13T15:48:49.620Z
2020-04-13T16:06:21.387Z
2020-04-13T16:06:21.387Z
user9975
<p>You can use negative numbers as the parameter for <code>segment</code> which then counts from right to left.</p>

<p>So <code>{{ craft.app.request.segment(-1) }}</code> will get the last segment.</p>

<p>And... <code>{{ craft.app.request.segment(-2) }}</code> will get the next to last segment</p>

CC BY-SA 4.0
2020-04-18T12:42:45.473Z
That's brilliant
CreateSean
CC BY-SA 4.0
2020-04-19T15:21:15.080Z
@foamcow thanks! i didn't know about that
user9975
CC BY-SA 4.0
2020-04-17T20:45:54.483Z
2020-04-17T20:45:54.483Z
foamcow
<p>I'm setting up a dynamic thank-you page for all forms added to the site. Client can add forms with freeform to any page and I have set the return url to this:</p>

<pre><code>{{ form.renderTag({
    class: "w-full pb-4",
    returnUrl: craft.app.request.absoluteUrl ~ 'thank-you/'
    }) }}
</code></pre>

<p>Which works. However on the thank-you page I would like to set the entry to be the slug of the <strong>second last url segment</strong></p>

<p>i.e. if the thank you page is <a href="https://domain.com/foo/bar/thank-you" rel="nofollow noreferrer">https://domain.com/foo/bar/thank-you</a> then I need to be able to get <strong>bar</strong> however if the url is <a href="https://domain.com/foo/thank-you" rel="nofollow noreferrer">https://domain.com/foo/thank-you</a> then I need to get <strong>foo</strong></p>

<p>How can I do this? if only <code>craft.app.getSecondLastSegment()</code> were a thing.</p>

<p>so ideally something simpler than this:</p>

<pre><code>{% set entrySlug = "" %}

{% if craft.app.request.getSegment('4')|length %}
  {% set entrySlug  = craft.app.request.getSegment('3') %}
{% elseif craft.app.request.getSegment('3')|length %}
  {% set entrySlug  = craft.app.request.getSegment('2') %}
{% elseif craft.app.request.getSegment('2')|length %}
{% else %}
  {% set entrySlug  = craft.app.request.getSegment('1') %}
{% endif %}```
</code></pre>

<templating><url><segments>
How to get second last url segment
# How to get second last url segment

(Asked by: username_0 on 2020-04-13)

I'm setting up a dynamic thank-you page for all forms added to the site. Client can add forms with freeform to any page and I have set the return url to this:
{{ form.renderTag({
    class: "w-full pb-4",
    returnUrl: craft.app.request.absoluteUrl ~ 'thank-you/'
    }) }}

Which works. However on the thank-you page I would like to set the entry to be the slug of the second last url segment
i.e. if the thank you page is https://domain.com/foo/bar/thank-you then I need to be able to get bar however if the url is https://domain.com/foo/thank-you then I need to get foo
How can I do this? if only craft.app.getSecondLastSegment() were a thing.
so ideally something simpler than this:
{% set entrySlug = "" %}

{% if craft.app.request.getSegment('4')|length %}
  {% set entrySlug  = craft.app.request.getSegment('3') %}
{% elseif craft.app.request.getSegment('3')|length %}
  {% set entrySlug  = craft.app.request.getSegment('2') %}
{% elseif craft.app.request.getSegment('2')|length %}
{% else %}
  {% set entrySlug  = craft.app.request.getSegment('1') %}
{% endif %}```

**Tags:** templating, url, segments

---

## 2 answers

### Answer by username_2 on 2020-04-17. Score: 10 | Accepted answer

You can use negative numbers as the parameter for `segment` which then counts from right to left.

So `{{ craft.app.request.segment(-1) }}` will get the last segment.

And... `{{ craft.app.request.segment(-2) }}` will get the next to last segment

> **Comments:**
> **username_0**: That's brilliant  
> **username_1**: @foamcow thanks! i didn't know about that  

---

### Answer by username_1 on 2020-04-13. Score: 1

{% if craft.app.request.segments|length > 1 %}
  {% set secondToLast = craft.app.request.segments|slice(-2, 1)|first %}
{% else %}
  no second to last
{% endif %}
{{ secondToLast }}

getSegments() returns an array of segments
in line 1: check if there is a second to last item
slice(-2, 1): -2 target second to last item and you only that one so second parameter is 1
|first is because slice returns an array

this solution need some tests and some consideration
for example Craft docs says:

Note that the segments will not include the control panel trigger if
  it’s a control panel request, or the page trigger or page number if
  it’s a paginated request.


---

