htaccess for URL remapping, caching, and compression I have been collecting snippets from all over the place and then put them all together creating the following .htaccess file.

I really don't understand if what I have done below is good/should work as expected, so your opinion is really appreciated.

I also don't understand the bit just after #Leverage Browser Caching.  Can anyone please explain it to me?

# Date 2 2 2014

ErrorDocument 404 /404

RewriteEngine On
RewriteBase /

    # Redirect if it begins with www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Remove enter code here.php; use THE_REQUEST to prevent infinite loops
    # By puting the L-flag here, the request gets redirected immediately
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301,L]

    # Remove /index
    # By puting the L-flag here, the request gets redirected immediately
    # The trailing slash is removed in a next request, so be efficient and dont put it on there at all
RewriteRule (.*)/index$ $1 [R=301,L]

    # Remove slash if not directory
    # By puting the L-flag here, the request gets redirected immediately
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]

    # Add .php to access file, but don't redirect
    # On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if
    # no such file exists. Be safe and add an extra condition
    # There is no point in escaping a dot in a string
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !(/|\.php)$
RewriteRule (.*) $1.php [L]

    # Change the sitemap extension from .php to .xml
RewriteRule ^sitemap.xml(.*)$ /sitemap.php?$1

    # Leverage Browser Caching
    # 480 weeks
&lt;FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"&gt;
    Header set Cache-Control "max-age=290304000, public"
&lt;/FilesMatch&gt;

    # 2 DAYS
&lt;FilesMatch "\.(xml|txt)$"&gt;
    Header set Cache-Control "max-age=172800, public, must-revalidate"
&lt;/FilesMatch&gt;

    # 2 HOURS
&lt;FilesMatch "\.(html|htm)$"&gt;
    Header set Cache-Control "max-age=7200, must-revalidate"
&lt;/FilesMatch&gt;

&lt;IfModule mod_deflate.c&gt;
    #The following line is enough for .js and .css
AddOutputFilter DEFLATE js css
AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

    #The following lines are to avoid bugs with some browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Leverage browser caching is designed to prevent browsers from using a cache for too long. If your website is dynamic, this is necessary to prevent users' browsers from storing your website for too long.

The way your file does this is by checking the type of file, and making a browser's cache of it expire after a specified amount of time. Images the other files you listed generally change little, so that part of you code is good unless you create dynamic images often. Your XML and TXT file deadline is okay unless you have XML news feeds or similar. HTML expiration date is also good as those files are usually the most often edited.

Overall, your file structure is good. I am sorry if this did not answer your question.

Edit:
Note that a browser doesn't have to obey you request to revalidate the page.