Назад | Перейти на главную страницу

.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", что заставляет меня думать, что он работает неправильно. Кто-нибудь знает, почему второе правило ни разу не применяется?

В итоге я пришел к следующему:

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

Что делает то же самое, но строит это наоборот.

# 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]