Regex: extract first and last child directories from URL

I need a regular expression that matches two virtual folders; the first and the last in a given URL:

www.example.com/first_folder/some/additional/bunch/of/folders/last_folder/

Read More

so far I have this regex: (?<!/)/[^/?#]+ but it matches all the folders 🙁

I need this for a rewrite rule to use in WordPress. A Quick and dirty example for rewriting http://mysite/project/1 into http://mysite/index.php?pagename=project&id=1:

$newrules = array();
$newrules['(project)/(d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';

Related posts

Leave a Reply

1 comment