需要正则表达式方面的帮助 - URL 重定向

发布于 2024-09-02 13:56:30 字数 323 浏览 3 评论 0原文

我正在尝试将一个易于记住的 url 重定向到 php 文件,但我在使用正则表达式时遇到了一些问题。

这是我目前所拥有的:

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

它可以工作,但前提是我提供所有 3 个参数。我希望最后两个参数是可选的,这样它要么只适用于第一个参数,要么适用于所有三个参数。我希望你能帮我解决这个问题。

非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2024-09-09 13:56:30

添加一个?在正则表达式中的某些内容使其成为可选之后。例如:

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

请注意,我围绕第二个和第三个参数引入了新的分组,因此必须移动反向引用。
您可能还想在末尾添加一个可选的 / ,这样就可以像指向目录一样使用它......

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

述情 2024-09-09 13:56:30

这是我解决问题的方法。对于可能偶然发现这个问题的人来说可能会有所帮助:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文