The simplest solution is to use PHP, as shown in other answers, but if you just want the regular expression to use with the .htaccess
file, 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 see that after path
there may be param
, query
and fragment
values. We start by analyzing only path
:
To get the last segment of path , we use the regular expression:
\/?([a-zA-Z0-9_\-+]+)\/?$
That is, the value can start with a slash, followed by a non-null sequence of letters, numbers or
_
,
-
and
+
(can freely change that part), followed or not by a slash , ending the value. In this way, the following URLs below will be properly analyzed:
edit
/edit
/edit/
/content/edit
/content/edit/
See working at Regex101 .
Now, we must add to the expression the part that will analyze the possible existence of params in the URL. For simplicity, since it is not in our interest to know the parameters of path , let's consider as a parameter any string other than /
that follows the ;
character. Both the character and the string will be optional, so 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 at Regex101 .
The same logic we will apply to the query of the URL, being defined as any string of characters 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 at Regex101 .
To complete, we need to parse the fragment part of the URL, being defined as any string that follows the #
character.
\/?([a-zA-Z0-9_\-+]+)\/?(?:\;[^\/]*)?(?:\?.*)?(?:\#.*)?$
This works 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 group caught will be edit
.
See working at Regex101 .
The same expression can be simplified to:
\/?([\w+-]+)\/?(?|\;[^\/]*|[?#].*)?$
Guilherme Lautert .
See working at Regex101 .