Forum Moderators: phranque
#Canonical force https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c> - is it required for some reason? RewriteCond %{HTTP_HOST} ^(.*)$ [NC] [edited by: not2easy at 12:48 am (utc) on May 20, 2020]
[edit reason] unlinked URL for readability [/edit]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L] It is below all other rules ...
2. What is this line supposed to do?:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
I don't see how every request would not meet that RewriteCond (if there were a [NC,OR] flag on the previous line)
this code checks to see if mod_rewrite is available
I took everything out of htaccess except the following and it still doesn't work:
Is this a WordPress site? That would explain why it ignores the htaccess rules at least.
I have the recent access log for when I was checking the redirects yesterday, and everything (my testing) is showing 200 response every time and only shows http://example.com. not with the "S" on https. or the www.
I'm not sure what else to look for however.
i would check to see if the www/non-www and http/https are all configured with the same document root directory.
it's possible the non-canonical hostnames are on different virtual hosts needing distinct .htaccess files.
The hosting company provided different code for forcing https and www and everything is working now.
How does this rule force HTTPS?
RewriteCond %{HTTPS} !on RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]