使用此 htaccess 创建一个可选的 Get 变量
我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会用两个单独的规则来编写它:
如果有 4 个路径组件,则第一个规则将匹配,并跳过下一个规则 (
[S=1]
)。否则下一条规则将尝试匹配。@Ulrich Palha 的解决方案可能也有效,但正则表达式变得越来越复杂。如果没有第四个路径组件,它将传递一个空的
step=
参数,这可能没问题。如果没有第四个路径组件,我的解决方案将不会传递step
参数。无论哪种方式都应该有效。I would write it with two separate rules:
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 nostep
parameter if there's no 4th path component. Either way should work.尝试
try