.htaccesshelicontech

rewriting a URL with not mandatory parts


Currently i have the following rewrite:

RewriteRule ([^/]*)/order/([^/]*)[/]? /order_form.php?lang=$1&plan=$2 [QSA,L]

which translates into: /en/order/ OR /en/order/plan1/

Which works fine. However, i'd like to add something on the back. Something like plan1-companyname.

But i can't seem to get it working.

i tried:

RewriteRule ([^/]*)/order/([^/]*)-companyname[/]? /order_form.php?lang=$1&plan=$2 [QSA,L]

but this makes it mandatory. when i include brackets

RewriteRule ([^/]*)/order/([^/]*)[-bucket/]? /order_form.php?lang=$1&plan=$2 [QSA,L]

it doesnt work either. Can someone fix me up or kickstart me?


Solution

  • You want to use parentheses instead of square brackets:

    RewriteRule ([^/]*)/order/([^/]*?)(-companyname)?/?$ /order_form.php?lang=$1&plan=$2 [QSA,L]
    

    Additionally, you need to make your second ([^/]*) grouping non-greedy.