I'm trying to set up a rule so requests coming from alternative domains are redirected to canonical domain, keeping request path and query string intact. I need to use .htaccess
because it's a shared hosting account.
Unfortunately, LiteSpeed documentation is extremely vague when it comes to what Apache features it supports, and this web server does not seem to spit errors, even if I type random text in configuration file.
None of these accomplish anything (response is sent normally through the requested domain):
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ https://example.com/$0 [R=301,L]
Can it be done with this or any other module?
Both rules work fine, both in Apache and in LiteSpeed.
The issue I was hitting is that the L
(or last
) flag of the RewriteRule
directive has a different behaviour in LiteSpeed, and I had a previous ruleset I hadn't mentioned in the question because I didn't think it was relevant.
This is not explained directly in the documentation itself but in the wiki, where it has a dedicated article — The Rewrite Rule [L] Flag:
- In most contexts, including LiteSpeed Web Server, this means that if the rule matches, no further rules will be processed. LSWS exits from the rewrite completely to avoid looping.
- In Apache, [L] means that if the rule matches, no further rules will be processed in the current iteration. Apache does not exit from the rewrite rule completely, and so there is a possibility of looping.
In plain English, this means that LiteSpeed stops processing mod_rewrite rules altogether as soon as a matching rule with L
flag is hit, instead of replicating the original Apache behaviour of restarting rule matching with the result of previous processing.
LiteSpeed is more intuitive here but, since it's supposed to mimic Apache, you don't expect that.
Wiki article provides a number of alternatives. What I did was:
Ensure all external redirections come first and have an L
flag.
Review that the order of internal redirections makes sense, and L
flags are placed only where you don't really want any other rule to be applied afterwards.