Forum Moderators: phranque

Message Too Old, No Replies

Browser caching using htaccess

         

gpweb

3:53 am on Oct 20, 2011 (gmt 0)

10+ Year Member



I am currently trying to take advantage of browser caching for images to speed my site load times and make firebug happy however I'm having a bit of an issue with the proper syntax. I have tried several different methods found in searches but to no avail all return a 500 error so I'm assuming due to my lack of knowledge its a syntax issue of my incorrectly constructing the instructions.
Server is Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4
Thanks in advance for any help or suggestions.

Below is my current working htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule ^(.*)$ [mysite.com...] [R=permanent,L]
RewriteBase /

RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-([0-9]+).html$ index.php?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-([0-9]+).html$ popup_image.php?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-([0-9]+).html$ product_reviews.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-([0-9]+).html$ product_reviews_info.php?products_id=$2&%{QUERY_STRING}

# End default subdomain redirect #
ErrorDocument 400 /v-web/errdocs/400.html
ErrorDocument 401 /v-web/errdocs/401.html
ErrorDocument 403 /v-web/errdocs/403.html
ErrorDocument 404 /v-web/errdocs/404.html
ErrorDocument 500 /v-web/errdocs/500.html



Here is an example of A tried AND FAILED addition

ExpiresActive On
ExpiresByType image/gif "A604800"
ExpiresByType image/jpg "A604800"
ExpiresByType image/png "A604800"
ExpiresByType image/bmp "A604800"

lucy24

5:09 am on Oct 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Aaaaack!

RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-([0-9]+).html$ index.php?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-([0-9]+).html$ popup_image.php?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-([0-9]+).html$ product_reviews.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-([0-9]+).html$ product_reviews_info.php?products_id=$2&%{QUERY_STRING}

It may "work", but it's also making your server do a horrendous lot of extra work.

You don't need &%{QUERY_STRING}. The standard flag [QSA] does the same thing.

Leave a blank line after each Rule so you can instantly see where you are.

Put an [L] flag at the end of each Rule unless you have a clear and specific reason not to. Here, each one is mutually exclusive, so [L] is appropriate.

And, finally, all literal periods . in the pattern need to be \. escaped.

The list of Error Documents should go at the very top, along with other "vanilla" directives like Options -Indexes.

Now, to take one at random:

^(.*)-p-(.*).html$ product_info.php?products_id=$2

Since you will not be using the part before -p-, and the code does not care what (if anything) is there, don't waste the server's time. Otherwise it goes

gobblegobblemunch to the very end--
oops! I was supposed to capture a -p-
backtrack, backtrack, until we can stop before the -p-
gobblegobblemunch to the very end--
oops! I need to leave room for .html
backtrack, backtrack...

And all of this happens on every single request, including the ones that were already matched in some preceding rule.

You want something more like

RewriteRule -p-([^.]+)\.html$ product_info.php?products_id=$1 [L,QSA]


excluding any horrible typos that g1smd will catch, so don't start cutting and pasting yet.

This is assuming you want Rewrites, rather than Redirects (using [R=301]). You probably do, but there may need to be a matching set of Redirects.

* * *
Yes, I realize this has nothing to do with your question. It was the ^(.*) that set me off.

gpweb

7:41 am on Oct 20, 2011 (gmt 0)

10+ Year Member



Thanks Lucy
I realized it probably didn't look pretty, the re-write rules are needed for a oscommerce addition called ultimate seo urls that takes the standard osc urls like

[yoursite.com...]

and changes them to:

[yoursite.com...]

When I first installed the contribution I had some help (apparently no so good)from someone on the forum here setting up the htaccess which is required to work with the seo urls and I remember it being a ton of trouble to finally get it working : (
I would be extremely happy to make a contribution to someones favorite charity (or to themselves) for assistance in now fixing the issues Lucy has pointed out and the browser caching of images : ) The htaccess at the top of this string is functioning so hopefully that gives a good clue how its intended to function.

g1smd

7:48 am on Oct 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't use (.*) at the beginning or in the middle of your patterns as this forces tens of thousands of "back off and retry" trial matches per request.

Lucy says, "you don't care what the first part is, you're not capturing and re-using it, so simplify the pattern".

You should be capturing those words and you should be comparing it to what the canonical format of that URL should be.

That is, you have a link to
example.com/this-great-product-p-423765


I could link to
example.com/do-not-buy-this-shoddy-junk-p-423765
and your site will return the page with 200 OK status.

You should capture $1, but using a pattern other than (.*) here, and the PHP script should make sure the words are correct. If not, the PHP script should issue a 301 redirect to the correct URL.

gpweb

1:26 am on Oct 21, 2011 (gmt 0)

10+ Year Member



Thanks for the assistance so far! And you are correct many of the seo url contribs do exactly as you stated which obviously could be bad if you had a mean competitor.
The php script we're using does issue a redirect, it looks at the number portion of the url so linking to example.com/do-not-buy-this-shoddy-junk-p-423765 immediately 301 redirects to example.com/this-great-product-p-423765.

Any help for the original question would be greatly appreciated I know your time and expertise is very valuable.

Here is one I have not tried yet but appears to do what we need, any thoughts?

# cache images for 1 month, do not require revalidation
<FilesMatch "\.(gif|jpe?g|png|flv|swf|ico)$">
Header set Cache-Control: "max-age=1296000"
</FilesMatch>
#
# cache scripts, css, and documents for 1 week, do not require revalidation
<FilesMatch "\.(js|css|pdf)$">
Header set Cache-Control: "max-age=604800"
</FilesMatch>