1

I recently did an URL spring cleanup in the backend and I'm currently struggling with one of the .htaccess redirects.

What I'm trying to redirect is:

https://www.example.com/directory/?type=supplier&category=celebrant&sort=latest

which should go to:

https://www.example.com/suppliers/type/celebrant

I've ended up with this regex and tested it on https://technicalseo.com/tools/htaccess/ but for some reason, my LiteSpeed Enterprise server doesn't seem to like it (other regex work).

RewriteCond %{REQUEST_URI} ^\/directory/$ [C]
RewriteCond %{QUERY_STRING} type=supplier&category=([a-zA-Z0-9-_]*) [C]
RewriteRule ^/?(.*)$ /suppliers/type/%1 [L,QSD,R=301]

Any thoughts on why it doesn't work with the enterprise version of LiteSpeed?

Update:

I did enable debug logging as George Wang suggested and what I got is:

unknown rewrite condition flag while parsing: RewriteCond %{REQUEST_URI} ^/directory/$ [C]
Invalid rewrite condition while parsing: RewriteCond %{REQUEST_URI} ^/directory/$ [C]
unknown rewrite condition flag while parsing: RewriteCond %{QUERY_STRING} type=supplier&category=([a-zA-Z0-9-_]*) [C]
Invalid rewrite condition while parsing: RewriteCond %{QUERY_STRING} type=supplier&category=([a-zA-Z0-9-_]*) [C]
Ignored due to previous error while parsing: RewriteRule ^/?(.*)$ /wedding-suppliers/type/%1 [L,QSD,R=301]

Pretty vague for me :(

Marc
  • 13
  • 5

2 Answers2

1
RewriteCond %{REQUEST_URI} ^\/directory/$ [C]
RewriteCond %{QUERY_STRING} type=supplier&category=([a-zA-Z0-9-_]*) [C]
RewriteRule ^/?(.*)$ /suppliers/type/%1 [L,QSD,R=301]

You need to remove the C (chain) flag. This is a RewriteRule flag, it doesn't make sense on the RewriteCond directive. (The C flag chains rules together, not directives.)

Apache would break with a 500 Internal Server Error, but I suspect LiteSpeed is probably just failing silently.

[a-zA-Z0-9-_]* - this regex/character class is incorrect, since the - (hyphen) needs to either be escaped or appear at the start or end of the character class. Otherwise it tries to calculate a range. This can also be simplified using the \w (word character) shorthand character class.

You can also remove the first condition and perform the URL-path check in the RewriteRule directive instead.

For example:

RewriteCond %{QUERY_STRING} ^type=supplier&category=([\w-]*)
RewriteRule ^directory/$ /suppliers/type/%1 [QSD,R=301,L]

If LiteSpeed doesn't like the QSD flag (new with Apache 2.4) then remove it and append a ? to the end of the substitution string. ie. ... /suppliers/type/%1? [R=301,L]

This redirect will need to go near the top of your .htaccess file.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • 1
    As per your instructions, I was able to fix the issue by changing the regex to what MrWhite put together. Thank you Just to confirm, [QSD] works in LiteSpeed Enterprise but also appending ? after %1 like in your example. And instead of my initial [a-zA-Z0-9-_]* I ended up using [\w'-]* just to include hyphens as well. Thank you! – Marc Feb 25 '21 at 10:00
0

You can try

RewriteCond %{REQUEST_URI} ^/directory/$ [C]

see if it helps or not. If not, you may need to turn on rewrite debug logging to find out what happened. :-)

  • To be honest, that was added by mistake after copy/paste on stackoverflow. The server had the original regex with no "\". Thank you and I will sure try to debug logging the following day. – Marc Feb 25 '21 at 00:38