How to get second last url segment
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