开发者

Need help with regular expressions - URL redirection

开发者 https://www.devze.com 2022-12-31 02:57 出处:网络
I\'m trying to redirect an easy to remember url to a php file but I\'m having some trouble with the regex.

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.


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...


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
0

精彩评论

暂无评论...
验证码 换一张
取 消