I have the following in my .htaccess file:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

It works as expected in the browser, redirecting all traffic to https://www.example.com/.

But I am using a POS system that automatically uploads products to the website and apparently this desktop app does not like more than one RewriteRule. It complains that it's web store is not installed. As soon as I remove one of the rules it is happy.

So my question is: Is there any way to consolidate that into a single RewriteRule?

1 Answer 1


...does not like more than one RewriteRule.

This requirement is unusual. It's probably complaining about the multiple redirects when requesting http://example.com, rather than literally the number of RewriteRule directives?

This could be resolved by making sure you redirect to the canoncial URL in the first RewriteRule directive. So, instead of:

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You would write:

RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Or, to keep this "generic", without explicitly including the domain name in the RewriteRule, try the following:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

Single RewriteRule

You can then extend the above so create a single RewriteRule for both the HTTPS and www canonicalisation. Try the following:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

For all requests that are either HTTP or do not start www then redirect to the canonical URL. The third RewriteCond directive is required to grab the domain (less an optional www. prefix) from the host. The %1 is a backreference to the last matched CondPattern ie. (.+). If you simply hard coded the canonical hostname in the RewriteRule substitution then you wouldn't need this third condition.

You must log in to answer this question.

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