【问题标题】:Combining multiple htaccess rewrite rules into one将多个 htaccess 重写规则合并为一个
【发布时间】:2010-01-15 22:18:34
【问题描述】:

首先,让我这样说:我很讨厌正则表达式和 htaccess。我正在尝试制定一个规则,将 url 段正确解析为变量。到目前为止,我有这个:

RewriteRule ^([^/]+)/?$ index.php?query[]=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3&query[]=$4 [QSA,L]

它确实有效,但我觉得它比它需要的更长;如果我想要 5 个或 6 个或 7 个变量怎么办?有没有更简洁的写法?

另外,当我吐出查询数组时,第一个元素始终是 index.php。这是怎么回事?

【问题讨论】:

    标签: php regex .htaccess


    【解决方案1】:

    不要使用RewriteRule 将pathinfo 转换为查询参数,而只需在Apache HTTP Server 中启用MultiViews 并在index.php 中使用$_SERVER['PATH_INFO'],而explode() 的帮助很少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2015-02-15
      • 2017-11-21
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多