: Is it okay for url to end .html even if stored gzipped? I have some html files stored on Amazon s3, some are quite large so the plan was to store them gzipped and return that as the encoding
I have some html files stored on Amazon s3, some are quite large so the plan was to store them gzipped and return that as the encoding type when receive a request for them, is that how it works rather than server having to encode them.
Can the file still end as .html even if in gzip format. I know the server should check what encodings the user/browser supports and then return the correct format but not sure if I have to code that bit of it is handled by Tomcat.
More posts by @Michele947
1 Comments
Sorted by latest first Latest Oldest Best
My question is why. Why use .html when you can use friendly URL's instead?
Anyways, technically, you could but the important thing that determines how the browser handles the data is the HTTP content-type header.
In a standard HTML document, the content type header will contain at least this:
Content-type: text/html
Some may go as far as this in order to specify the character encoding of the page:
Content-type: text/html; charset=UTF-8
With regards to serving gzipped html pages, the proper way to do it is to first compress the HTML in a gzip format, then output the compressed data after the usual HTTP headers including the following headers at minimum:
Content-type: text/html
Content-Encoding: gzip
For browser compatibility, you can also include:
Vary: Accept-encoding
That way, you can serve compressed and uncompressed content. Here's code in php to help you understand things better. Feel free to save it as a file with a PHP extension, and yes the compression will still work:
<?php
$myhtml=file_get_contents("/path/to/plain/uncompressed/htmlfile.html");
header("Content-type: text/html",true);
header("Vary: Accept-Encoding",true);
if (strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']),"gzip") > 0){
//browser supports gzip
header("Content-Encoding: gzip",true);
$myhtml=gzencode($myhtml,2);
}
header("Content-length: ".strlen($myhtml),true);
flush();
echo $myhtml;
exit();
?>
First, a file is fetched locally in the server and the contents of it are stored in a myhtml variable. Next, the minimum headers are prepared. Then the system checks to see if browser supports gzip extraction by checking the incoming accept-encoding header. If it does, then the gzip encoding HTTP header is prepared and contents are myhtml are then compressed. Finally, regardless of the support for gzip, the length of the document is based on the number of bytes of the contents of myhtml. Finally its printed on screen.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.