.htaccess RewriteRule 没有按照我的意愿进行拆分

.htaccess RewriteRule 没有按照我的意愿进行拆分

因此,我想转换以下 URL:

tags/tag1/tag2/tag3

变成一个参数化的,例如:

tags.php?tags=tag1&tags=tag2&tags=tag3

这让我得到了以下 RewriteRules(以及其他规则)

RewriteRule ^tags/([^/]+)(/.+)?$ tags.php?tags=$1$2 [C]
RewriteRule ^tags.php?tags=([^/]+)/([^/]+)(/.+)?$ tags.php?tags=$1&tags=$2$3 [N]

但是当我使用 php 提取标签变量时,我得到了"tag1/tag2/tag3",这让我相信它没有正确运行。有人知道为什么第二条规则甚至没有被应用一次吗?

答案1

# Mostly the same as your first one - tweaked to allow for the second capture
# to be empty (1 tag) and to allow for (and ignore) a trailing slash:
RewriteRule ^tags/([^/]+)(/?.*)/?$ tags.php?tags=$1$2 [C]

# Again, minor tweak, allowing a blank string in the third match so the last
# run of the mangling (when you have `tags=tags1&tags=tags2/tags3`) doesn't come
# back with no match.
RewriteCond %{QUERY_STRING} ^tags=([^/]+)/([^/]+)(/?.*)$

# Match the file exactly; only happens if the condition matched (if a slash
# was found in the query params)
RewriteRule ^tags.php$ tags.php?tags=%1&tags=%2%3 [N]

答案2

我最终选择的是:

RewriteRule ^tags/?$ tags.php [L]
RewriteRule ^tags/&(tags\[\]=.*)$ tags.php?$1 [L]
RewriteRule ^tags/([^/]+)(/?.*)$ tags/$2&tags[]=$1 [L]

它的作用是相同的,但是是以相反的方式构建的。

相关内容