Forum Moderators: phranque
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
ErrorDocument 404 https://www.example.com/custom404.html
# Permanent URL redirects
Redirect 301 /about.html https://www.example.com
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L] RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L] RewriteRule ^(([^/]+/)*)index\.htm http://www.example.com/$1 [R=301,L,NS]
See the [NS] flag? That's for "no subrequest", meaning "do not invoke this rule-- and don't evaluate Conditions-- when there has been an internal request for the file 'index.html'" (invoked by the DirectoryIndex directive, which is the whole reason the / form works at all). RewriteCond %{REQUEST_URI} ^/(([^/]+/)*)index\.htm
RewriteRule index\.htm http://www.example.com/%1 [R=301,L,NS]
This two-steps-forward, one-step-back approach means that the server doesn't need to do any capturing except in the rare case where the request actually involves "index.html" and then only when it wasn't an internal subrequest. It may be a teensy weensy bit more efficient, since it's a relatively complicated capture with a bit of back-and-forthing.