http(s)://(www.)example.info
-> https://example.com
or https://www.example.com
On reading your question it doesn't sound like you want to canonicalise the www subdomain? However, you should be. Decide whether it should be www.example.com
or example.com
and stick to that. For the rest of this answer, I'll assume www.example.com
should be the canonical domain.
The easiest way would be to say... if the request is not for the canonical domain then redirect to the canonical domain. This will then catch any of the other TLDs and whether they are www or not (and even whether they are FQ or not).
For example:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
Notes about the above:
- Addition of the
OR
flag on the first condition. So either of the first two conditions must be successful (not just both of them).
- The
=
prefix defines a lexicographical exact string comparison. ie. not a regex. So no need to escape literal dots or use start/end-of-string anchors.
- Removal of the parentheses on the
RewriteRule
pattern. No need to capture anything, since you aren't using it.
- Added the flags on the
RewriteRule
. This should ultimately be a 301 permanent redirect, although first testing with a 302 temporary redirect (to avoid caching) is preferable. If you ever add more directives then you will need the L
flag to stop further processing.
- Add back the two cPanel additions if you wish (required for AUTO-SSL renewal). Or these can be separated into their own rule.
Can I wrap the previously described redirects in a conditional that will skip them if the server is localhost
?
There are quite a few ways to block the directives from local development (which can depend on your server config / version). If you simply want to block this single mod_rewrite rule from applying to the Host localhost
then just add another condition:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
ie. Only when the Host
header is not "localhost".
Summary
So, in summary, adding back the cPanel conditions (for AUTO-SSL renewal), we have:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
However, also note the comment by @xsrf if you are planning on implementing HSTS.
See also my answer to the following ServerFault question for more information regarding those additional directives (AUTO-SSL renewal) added by cPanel:
https://serverfault.com/questions/884915/apache-htaccess-multiple-rewrite-conditions-for-multiple-rules
RewriteOptions inherit
directive for?RewriteOptions Inherit
"inherits" mod_rewrite directives from a parent config (by literally copying them to the end of your current.htaccess
file). This could be relevant, particularly since you don't have anL
flag on your original directives. This directive shouldn't normally be required, especially if you don't actually know what it's doing. However, some hosts might require this.~/public_html/example
and~/public_html
also had an .htaccess file. If I understand you correctly it's likely thatRewriteOptions Inherit
is no longer necessary.RewriteOptions Inherit
is probably not required anymore.