Reputation: 49
I am trying to find out how can I write rewriterule in htaccess with an optional part. Normally I have this:
RewriteRule ^([a-z\-]+)/([A-Za-z\-]+)/([A-Za-z\-]+)/([0-9]+)/([0-9]+)/([0-9]+)\-([A-Za-z0-9\-]+)$ /system.php?lang=$1&category=$2&subcategory=$3&year=$4&month=$5&id=$6&title=$7 [QSA,L]
It works fine if I have address e.g.
http://dev.example.com/en/blog/usa/2013/9/1-title-of-post
The result is:
$_GET["lang"]="en";
$_GET["category"]="blog";
$_GET["subcategory"]="usa";
$_GET["year"]=2013;
$_GET["month"]=9;
$_GET["id"]=1;
$_GET["title"]="title-of-post";
But sometimes I just want to use address like:
http://dev.example.com/en/about/me
And I want this: (I mean not define other values, but server find that address)
$_GET["lang"]="en";
$_GET["category"]="about";
$_GET["subcategory"]="me";
$_GET["year"]=null;
$_GET["month"]=null;
$_GET["id"]=null;
$_GET["title"]=null;
So I want optional part starting with year, but if someone writes address with year and other statements I want to take it.
Thank you for yout help!
Upvotes: 0
Views: 298
Reputation: 12323
Based on the new information, I think I understand what you're trying to do. I believe this will work:
RewriteRule ^([a-z\-]+)/([A-Za-z\-]+)/([A-Za-z\-]+)(/([0-9]+)/([0-9]+)/([0-9]+)-([A-Za-z0-9\-]+))?$ /system.php?lang=$1&category=$2&subcategory=$3&year=$5&month=$6&id=$7&title=$8 [QSA,L]
Note the changed numbers in the match variables ($1, etc.). You need one throwaway match group to make the second part optional, so the numbering skips one. Year is now $5, and everything after that shifts by 1.
Upvotes: 1