views:

36

answers:

2

Hello everyone.

I'm trying to redirect an easy to remember url to a php file but I'm having some trouble with the regex.

Here's what I have at the moment:

RewriteRule ^tcb/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}) /tcb/lerbd.php?autocarro=$1&tipo=$2&dsd=$3

It is working but only if I supply all 3 arguments. I want the last two arguments to be optional so it either works with only the first or all three. I'm hoping you can help me with this.

Thank you very much.

+1  A: 

Adding a ? after something in a RegEx makes it optional. so something like:

RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))? /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4

Notice I introduced a new grouping around the 2nd and 3rd arguments, so the backreferences had to be shifted. You might also want to put an optional / at the end, too, so it can be used just as if it pointed to a directory...

grossvogel
Hey. Thanks so much but it isn't working :( I had heard of the ? before but I couldn't get it to work. Thanks for the '/' in the end tip too, I'll work on it when I figure this first issue.
paperless
Hey, I tried the updated version and still no luck. It gives me the Internal Server Error :( I have no idea of what could be wrong because that really does look close to what might be the solution.
paperless
Nevermind, It was missing the $. How silly of me.
paperless
A: 

Here's how I solved the problem. It could be helpful for someone who might stumble upon this question:

RewriteRule ^tcb/([a-zA-Z0-9]{1,})/?(([a-zA-Z0-9]{1,})/([a-zA-Z0-9]{1,}))?$ /tcb/lerbd.php?autocarro=$1&tipo=$3&dsd=$4
paperless