regex.htaccessmod-rewriteurl-rewritinglitespeed

htaccess migration issue from Apache to LiteSpeed


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 :(


Solution

  • 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.