1

Old version of filters and sorting was creating links like (explenation of every part is below - here is example for same category with different views and filters used):

https://example.com/category-one/1/default/1/f_at_175_3/1
https://example.com/category-two/1/full/3/f_at_185_1/1
https://example.com/category-three/1/default/1/f_at_175_3/1/f_producer_7/1
https://example.com/category-name-and-size-40x40/1/default/1/f_at_175_3/1/f_producer_7/1

We need to redirect them to new link:

https://example.com/category1/1/

Here is my code so far. When I use "htaccess checker" tools, when I use flag "R=301" - there is error like below - so is it even possible to use 301 in this case?:
"Invalid line:RewriteRule is invalid, does it contain a space in the pattern or the substitution?"

RewriteEngine on
RewriteRule ^([a-z0-9-]+)/[0-9]{1,2}/(default|full)/[1-9](/f_[A-Za-z]+_[0-9]{1,3}(_[0-9]{1,2})?/1)+/?$ /$1/ [R=301, L]

detailed info:

Here is structure of these links:

https://example.com/category/A/default/B/f_filter_C_D/E
  • category - unique for every category name

  • A - number of currently viewed page, so we can have for example 10 pages of category X - in this case here appears 1-10 numbers

  • default - is defining type of view (it's grid or list), and there are only 2 options: "default" and "full"

  • B - ID of sorting (by name, price etc.), there are only 4 types of sorting (1,2,3,4)

  • f_ - is fixed part that appears everytime you choose any filter

  • filter - is defining type of filter used, there are some kind of types of this (at, producer, color etc.)

  • C - ID of filter, here occur various numbers (1-999)

  • D - I am not sure what this number do, but it appears always after "filter" part (in range of 1-99)

  • E - unknown number, but here is always "1"

There are no other directives that are referring to category links inside .htaccess file.

2 Answers 2

1
RewriteRule ^([a-z0-9-]+)/[0-9]{1,2}/(default|full)/[1-9](/f_[A-Za-z]+_[0-9]{1,3}(_[0-9]{1,2})?/1)+/?$ /$1/ [R=301, L]

This directive is "invalid" because it contains a space in the RewriteRule flags (3rd) argument [R=301, L] - it should be [R=301,L]. Spaces are delimiters in .htaccess, so this essentially breaks the 3rd argument in two. (The error being reported from that tool was not strictly correct or vague at best.)

However, you're not capturing the 2nd path segment ie. 1 (or A in your structure breakdown). So, this would redirect to /category1/, not /category1/1/, as in your example.

Otherwise, your directive/regex seems to be OK.

Try the following instead at the top of your .htaccess file.

RewriteRule ^([\w-]+/\d{1,2}/)(default|full)/\d(/f_[a-zA-Z]+_\d{1,3}(_\d{1,2})?/1)+$ /$1 [R=302,L]

Additional notes:

  • The $1 backreference now includes A and the trailing slash, so the trailing slash should be omitted from the substitution string.
  • I've removed the optional trailing slash on the pattern, since is not present on your example URLs or in your description of the URL format.
  • I used the shorthand character class \w to match the category. This matches A-Z and _ in addition to the characters you had stated, but it is slightly shorter.
  • I used \d instead of [0-9] (and [1-9]) - just slightly shorter.

You will need to clear your browser cache before testing. Test first with 302 (temporary) redirects to avoid erroneous caching issues.

UPDATE: It seems (from comments) that the "page number" (2nd path segment) should form part of its own URL parameter in the target URL (not an additional path segment, following on from the category), so this needs to be captured separately. And it seems the trailing slash is not required.

So, this should be changed to:

RewriteRule ^([\w-]+)/(\d{1,2})/(default|full)/\d(/f_[a-zA-Z]+_\d{1,3}(_\d{1,2})?/1)+$ /$1?page=$2 [R=302,L]

You'll also need to add the QSA flag if there might be an additional query string that needs to be passed through to the target URL.

0
0

Right, I didn't mention that category names also contains - - not _. Somteimes there are also size numbers. I edited my examples in previous post.

Also, I forgot about redirecting to specific number of page, so here is new version:

RewriteEngine on
RewriteRule ^([a-z0-9-]+)/(\d{1,2})/(default|full)/[1-9](/f_[A-Za-z]+_\d{1,3}(_\d{1,2})?/1)+/?$ /$1?page=$2 [R=301,L]

So now we are redirecting from:
/category-name-40x40/16/default/1/f_producer_41/1/f_at_173_23/1
to:
/category-name-40x40?page=16

3
  • The character class [\w-] (in my answer) already matched - (hyphen) and numbers/digits. The fact that it also matched _ (underscore) should not be an issue here?
    – MrWhite
    Mar 5, 2020 at 10:40
  • Right, [\w-] in this case is ok. I wanted to make sure about that before, and at site I visited they said "alphanumeric characters plus underscore" - I thought this class is not including - and numbers. Thanks for help, @MrWhite!
    – oska
    Mar 5, 2020 at 12:08
  • Yes, \w by itself does not include -, but [\w-] does.
    – MrWhite
    Mar 5, 2020 at 12:09

You must log in to answer this question.

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