Filtering Search Results by Category
In this line here (reformatted for legibilty):

&lt;a href="{{craft.request.url}}/?category={{ category.slug }}"&gt;
  {{ category.title}}
&lt;/a&gt;


You are generating the url by from craft.request.url and appending the category. But the original query is not part of the url at this point. I think this will work and be clearer:

&lt;a href="{{ url(craft.request.path, {q: query, category: category.slug}) }}"&gt;
  {{ category.title}}
&lt;/a&gt;


This uses the url function, passing it the query string parameters.