Forum Moderators: phranque
ErrorDocument 404 https:www.example.com/missing.htm
AddHandler server-parsed .htm RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ https://www.example.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] [edited by: phranque at 9:43 pm (utc) on Sep 4, 2018]
[edit reason] fix quote codes [/edit]
ErrorDocument 404 https:www.example.com/missing.htm
ErrorDocument 404 /missing.htm
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.com/$1 [R,L]
phranque: i would suggest this instead:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(example\.com)?$ [NC,OR]
This should be OK, providing you don't use any other subdomains (or domains) that resolve to the same place. Since it redirects everything to "www.example.com", it doesn't simply canonicalise "example.com". This is not a problem with your original rule (or my suggestion above).
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC,OR] However, the first RewriteCond directive is a bit confusing/ambiguous-looking and should be simplified IMO. The first condition uses a negated pattern that is also entirely optional.
For any legitimate request the host is always non-empty, so it's always successfulwhitespace, look again. The negation means “if the host is NOT (exactly suchandsuch OR exactly nothing)”. That’s why the opening and closing anchors are essential.