Does htaccess intercept pass two variables or one dependent if available?

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

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

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


How can I only pass category1 if category2 is not available?


source to share

2 answers

Several solutions:

A few rules (easier to understand, but a little slower):

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


Uniform rule:

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


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



Maybe try this:

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




All Articles