.htaccess正则表达式简化

时间:2013-10-16 05:09:32

标签: regex .htaccess

我重写了.htaccess的一部分,如:

RewriteRule ^content/([^/]+)/([^/]+)/?$ /index.php?section=clanky&page=$1&id=$2 [L]
RewriteRule ^content/([^/]+)/?$ /index.php?section=clanky&page=$1 [L]
RewriteRule ^content/?$ /index.php?section=clanky [L]

有没有办法让它更容易修改(一行)?

非常感谢

1 个答案:

答案 0 :(得分:1)

只要您不介意在php $_GET数组中获取空​​白参数,就可以这样做:

RewriteRule ^content/?(?:([^/]+)/?|)(?:([^/]+)/?|)$ /index.php?section=clanky&page=$1&id=$2 [L]

这意味着如果URI如下所示:

/content/123/asd

$_GET数组将是:

Array
(
    [section] => clanky
    [page] => 123
    [id] => asd
)

如果是这样的话:

/content/qwe

你会得到:

Array
(
    [section] => clanky
    [page] => qwe
    [id] => 
)

如果是的话:

/content/

然后:

Array
(
    [section] => clanky
    [page] => 
    [id] => 
)