The simplest solution is using PHP, as presented in other answers, but if you only want the regular expression to use next to the file .htaccess
, see the solution below.
To answer this, we first need to understand the structure of a URI:
foo://example.com:8042/over/there;param=value;p2;p3?name=ferret#nose
\_/ \______________/\_________/ \_______________/ \_________/ \__/
| | | | | |
scheme authority path params query fragment
We can realize that after the path
the values of param
, query
and fragment
. We then begin by analyzing only the path
:
To get the last segment of path, we use regular expression:
\/?([a-zA-Z0-9_\-+]+)\/?$
That is, the value can start with a slider, followed by a non-zero sequence of letters, numbers or _
, -
and +
(can freely change this part), followed or not by a bar, ending the value. In this way, the following Urls below will be properly analyzed:
edit
/edit
/edit/
/content/edit
/content/edit/
See working on Regex101.
Now, we must add to the expression the part that will analyze the possible existence of params in the URL. To simplify, as it is not of our interest to know what are the parameters of the path, let’s consider as parameter any string other than /
that follows the character ;
. Both the character and the sequence will be optional, then the regular expression becomes:
\/?([a-zA-Z0-9_\-+]+)\/?(?:\;[^\/]*)?$
So both the Urls above and below will work:
edit
/edit
/edit/
/content/edit
/content/edit/
/content/edit;param=foo
/content;param=foo/edit/
See working on Regex101.
The same logic will apply to the query URL, being defined as any string that follows the character ?
. Thus the regular expression becomes:
\/?([a-zA-Z0-9_\-+]+)\/?(?:\;[^\/]*)?(?:\?.*)?$
So all the Urls below will work:
edit
/edit
/edit/
/content/edit
/content/edit/
/content/edit;param=foo
/content;param=foo/edit/
/content/edit?q=foo
/content/edit/?q=foo
See working on Regex101.
In addition, the Fragment URL, being defined as any string that follows the character #
.
\/?([a-zA-Z0-9_\-+]+)\/?(?:\;[^\/]*)?(?:\?.*)?(?:\#.*)?$
Thus working for all possible URL variations:
edit
/edit
/edit/
/content/edit
/content/edit/
/content/edit;param=foo
/content;param=foo/edit/
/content/edit?q=foo
/content/edit/?q=foo
/content/edit#foo
/content/edit/#foo
In all, the only captured group will be edit
.
See working on Regex101.
The same expression can be simplified to:
\/?([\w+-]+)\/?(?|\;[^\/]*|[?#].*)?$
Collaboration of Guilherme Lautert.
See working on Regex101.
What are the expected results? In all the word
edit
shall be returned or in the latter the part?q=
should also be present at the return?– Woss
in vdd only the Edit and ignore the variables
– Ricardo Rauber
So it’s not necessarily between the last occurrences of
/
, but always the last segment of path?– Woss
That’s it! But I just need the last word, in the Edit case, that could be any other.
– Ricardo Rauber
It is that with each issue the question gets more confused. In a URL
edit?q=teste
, the last word would beteste
, but what you need isedit
, correct? If yes, use the expression last segment of the path, so as not to create any confusion.– Woss
You’re right, I made the changes
– Ricardo Rauber
Put exactly like this your whole . htaccess, because multiple rewrites can conflict.
– Guilherme Nascimento