2011-11-22 70 views
0

我有这样的规则在我的.htaccess:请可选获取变量,这htaccess的

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

这对于这个网址的伟大工程:

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

但现在我需要一个可选的4 Get变量以这个规则,这将给我$_GET像这样的可变步骤:

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

$_GET['step'] // 2 

Bu t如果没有第四个Get变量,我还需要规则才能工作。所以基本上我需要3和4 Get变量工作,使第四个可选。

回答

1

我会把它与两个单独的规则写:

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参数。无论哪种方式应该工作。

+0

什么是([S = 1]) – Trace

+0

它的意思是“跳过下一条规则,如果这一匹配”。请参阅[RewriteRule文档](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule)。 –

1

尝试

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