(Recursive) Regular expression for .htaccess file


I'm trying to modify my web page, which I currently use QueryStrings to display pages, and want to change website URLs to "search engine friendly" ones.

I'm a newbie in regular expressions and also in .htaccess file, and I want to change URLs from




Categories are stored recursively on the database so, I have to display each category level in URL. But what I need is only the last category in the url. for instance the category may be a root category like:


which is currently displayed from the URL:


or it may be a sub-category like:


which is currently displayed from the URL:


or may be a leaf like:

http://someaddress.bla/en/Company/AboutUs/Staff/.../Steve where "Steve" is the leaf in recursion. This page is currently displayed from the URL:


I couldn't write the regular expression to handle this situation, any help will be appreciated. Thanks!

By : fsonmezay


You can match the last item in the URL by anchoring to $ (the "end of string" character):


will match and capture the last set of characters in the URL that is not a / character.

By : Amber

RewriteRule ^/?([^/]+)/([^/]+)/?$ /?lang=$1&RootCategory=$2
RewriteRule ^/?([^/]+)/([^/]+)(/.+)?/([^/]+)/?$ /?lang=$1&RootCategory=$2&LeafCategory=$4

This video can help you solving your question :)
By: admin