1

So, I'd like to turn the following url:

tags/tag1/tag2/tag3

into one that is parameterized like:

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

which has left me with the following RewriteRules (among others)

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

but when I pull up the tags variable using php I get "tag1/tag2/tag3", which leads me to believe it is not running correctly. Anyone have any idea why the second rule isn't even getting applied once?

2
  • 2
    RewriteRule CANNOT match query string directly -- only with help of RewriteCond. RewriteRule matches path part of URL only (e.g. for URL tags.php?tags=tag1&tags=tag2&tags=tag3 it will match tags.php part only). To work with query string use RewriteCond %{QUERY_STRING} regex-pattern-here -- check docs httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond . If not sure -- enable rewrite debugging (RewriteLogLevel 9) and see rewrite log for details.
    – LazyOne
    Aug 16, 2011 at 1:16
  • That's likely my issue then. Maybe I'll figure out how to write it in reverse. Thanks Aug 16, 2011 at 1:21

2 Answers 2

0
# 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]
1
  • I couldn't get this to work for me and I don't have the ability to turn on logging through my host. I still get tag1/tag2 Aug 16, 2011 at 17:53
0

What I ended up going with was:

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

Which does the same thing but builds it in reverse.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .