3

I want to pass first directory as a variable and subdirectory as another variable.

It works when for urls with both directories (/something/something2) but I get an error 404 when I try with only one (/something).

RewriteRule ([^/]+)/([^/]+) /posts/?category2=$1&category2=$2 [L,QSA]

How can I pass only category1 when category2 isn't available?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

2 Answers2

3

Few solutions:

Multiple rules (easier to understand but a bit slower):

RewriteRule ^([^/]+) /posts/?category2=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+) /posts/?category2=$1&category2=$2 [L,QSA]

Single rule:

RewriteRule ^([^/]+)(/([^/]+))? /posts/?category2=$1&category2=$3 [L,QSA]

Here the second part of URL (/something2) is optional. Please note -- $2 was changed to $3.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
0

Maybe try this one:

RewriteRule ([^/]+)(/([^/]+))? /posts/?category2=$1&category2=$3 [L,QSA]
animuson
  • 53,861
  • 28
  • 137
  • 147
piotrekkr
  • 2,785
  • 2
  • 21
  • 35