3
\$\begingroup\$

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
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
    Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

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

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

<IfModule mod_deflate.c>
    #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
\$\endgroup\$
4
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This is a great answer thank you, however how can I change the time? or what is the actually dead-time? i.e when stuff die? \$\endgroup\$ – Leo Feb 18 '14 at 16:03
  • \$\begingroup\$ Your comments reveal this. If 7200 means two days, as shown in your comments, then 3600 means one day. This is when the browser will request the page again instead of accessing a cache. \$\endgroup\$ – xv435 Feb 18 '14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.