Forum Moderators: phranque
# Setup
RewriteEngine On # Fix all .html requests to redirect to www and .htm
RewriteRule (.*)\.html$ http://www.example.com/$1.htm [R=301,L] # Fix non-www requests to redirect to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1.htm [R=301,L] This is about the simplest ever .htaccess file.
I guess you'll need to add lines for
ErrorDocument and DirectoryIndex and Options -Indexes settings too. Read the Apache documentation to find out what they do.
#Rewrite index.html to homepage without index.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
AND I already have a non-www to www, but it says nothing about .htm or .html. Do I need to add your rule in addition to the rule below?
# Rewrite non-www to www
RewriteCond %{HTTP_HOST} ^example.(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
It looks like I need all 4 rules (your two, plus my existing two).
# Redirect index.html or .htm in any directory to root of that directory and force www
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html?[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1? [R=301,L] This rule must redirect to / for both index.html and index.htm requests in order to avoid a redirection chain if index.html is requested.
The three new rules replace all of your code.
# Setup
RewriteEngine On
#
# Redirect all .html requests to .htm on canonical host
RewriteRule ^(.*)\.html$ http://www.example.com/$1.htm [R=301,L]
#
# Redirect direct client requests for "index.html" to root URL "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
#
# Redirect non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I had already added a rule just above his index.html redirect that further redirects requests for index.htm to canonical:
# Redirect direct client requests for "index.htm" to root URL "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.htm
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
followed by
# Redirect direct client requests for "index.html" to root URL "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
So basically, I have 4 rules instead of his 3. Everything seems to be resolving on all variants I have tested.
Also, what happens when using a '+' versus {3,9}?
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.htm Versus
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.htm
One rule, with a pattern of
index.html[b]?[/b] will take care of both requests and be a lot more efficient. For your other problem, use Live HTTP Headers, check what MIME-type is returned for that content. It is likely incorrect or missing.
The '+' means 'not blank' and the '{3,9}' means 'between three and nine characters'. Mostly personal style I guess.
The index rules must be first, next the more general .html rules, and lastly the general non-www to www rules.