使用此 htaccess 创建一个可选的 Get 变量

发布于 2024-12-17 23:39:37 字数 563 浏览 0 评论 0原文

我的 .htaccess 中有此规则:

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3

这对于此 url 非常有用:

http://somesite.com/po_systems/build_system/60/495C31/1

但现在我需要此规则的可选第四个 Get 变量,该变量将为我提供 $_GET 变量步骤如下:

http://somesite.com/po_systems/build_system/60/495C31/1/2

$_GET['step'] // 2

但如果没有第四个 Get 变量,我还需要该规则起作用。所以基本上我需要 3 个和 4 个 Get 变量才能工作,因此第 4 个变量是可选的。

I have this rule in my .htaccess:

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3

Which works great for this url:

http://somesite.com/po_systems/build_system/60/495C31/1

But now I need an optional 4th Get variable to this rule that will give me the $_GET variable step like this:

http://somesite.com/po_systems/build_system/60/495C31/1/2

$_GET['step'] // 2

But I also need the rule to work if there is no 4th Get variable. So basically I need both 3 and 4 Get variables to work, making the 4th optional.

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

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

发布评论

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

评论(2

雨巷深深 2024-12-24 23:39:37

我会用两个单独的规则来编写它:

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4 [S=1]
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$         /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3

如果有 4 个路径组件,则第一个规则将匹配,并跳过下一个规则 ([S=1])。否则下一条规则将尝试匹配。

@Ulrich Palha 的解决方案可能也有效,但正则表达式变得越来越复杂。如果没有第四个路径组件,它将传递一个空的 step= 参数,这可能没问题。如果没有第四个路径组件,我的解决方案将不会传递 step 参数。无论哪种方式都应该有效。

I would write it with two separate rules:

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4 [S=1]
RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?$         /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3

If there are 4 path components, the first rule will match, and skip the next rule ([S=1]). Otherwise the next rule will try to match.

@Ulrich Palha's solution probably also works, but the regular expression is getting complicated. It will pass an empty step= parameter if there's no 4th path component, which may be fine. My solution will pass no step parameter if there's no 4th path component. Either way should work.

倾城花音 2024-12-24 23:39:37

尝试

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?([^/]*)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4

try

RewriteRule ^build_system/([^/]+)/([^/]+)/([^/]+)/?([^/]*)/?$ /po_systems/build_system.php?business_id=$1&system_id=$2&quantity=$3&step=$4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文