(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
http://someaddress.bla/index.php?lang=en&rootCategory=$rootCaty&leafCategory=$leafCat
to
http://someaddress.bla/en/$rootCat/$sub-category/$sub-category2/.../$leafCat
Categories are stored recursively on the database so, I have to display each category level in URL. Bu开发者_JAVA百科t what I need is only the last category in the url. for instance the category may be a root category like:
http://someaddresss.bla/en/Company
which is currently displayed from the URL:
http://someaddress.bla/?lang=en&RootCategory=Company
or it may be a sub-category like:
http://someaddress.bla/en/Company/AboutUs
which is currently displayed from the URL:
http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=AboutUs
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:
http://someaddress.bla/?lang=en&RootCategory=Company&LeafCategory=Steve
I couldn't write the regular expression to handle this situation, any help will be appreciated. Thanks!
RewriteRule ^/?([^/]+)/([^/]+)/?$ /?lang=$1&RootCategory=$2
RewriteRule ^/?([^/]+)/([^/]+)(/.+)?/([^/]+)/?$ /?lang=$1&RootCategory=$2&LeafCategory=$4
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.
精彩评论