HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

htaccess for URL remapping, caching, and compression

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
compressionhtaccesscachingforandremappingurl

Problem

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

Header set Cache-Control "max-age=290304000, public"

# 2 DAYS

Header set Cache-Control "max-age=172800, public, must-revalidate"

# 2 HOURS

Header set Cache-Control "max-age=7200, must-revalidate"

#The following line is enough for

Solution

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.

Context

StackExchange Code Review Q#41964, answer score: 4

Revisions (0)

No revisions yet.