Using htaccess to bundle a bunch of files into a single one - .htaccess

Say you have a css files loader style.php:
<?php
header('Content-type: text/css');
foreach(array('style1.css', 'style2.css', 'style3.css') as $f)
echo file_get_contents($f)
?>
Style1.css has 12KB, style2.css is 400kgs, and in the red corner obese style3.css weighting 800LBs is world champion at static resource bandwidth consumption!
I'm using style.php to combine the three files and send them to the client. I'm also using similar php files to send out JS resources, combined.
Is there some htaccess rule that I can tell to combine several static resources into a big one, and send that on-the-fly?
/EDIT:
This type of job CAN be handled by htaccess I'm sure I've read somewhere about server files included or something like that but I don't remember where. And I've also seen free hosting services that put a custom header or banner regardless of what files you host there.

Well this type of job (combining css files) cannot be handled by .htaccess. You can at best use mod_deflate to compress the css file's contents.
However in PHP code you can combine and compress various CSS files. Take a look at third method in http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php
Finally take a look at minify here: http://www.minifycss.com/minify-tools/minify-css-tools.php

Eventually found what I was looking for. The thing was called Server-Side Includes (SSI).

Related

How do I serve MathJax from a local Happstack server?

I'm not a developer/programmer. I'm just someone trying to use Gitit to take notes. I've got it to the point where it runs on Windows, but the math looks best using MathJax. I don't want to rely on a remote CDN to get the MathJax working (power cuts and internet disconnections are very frequent here). The author of the app mentions it can be setup in "4 lines of code" in Happstack:
mathjax-script: https://d3eoax9i5htok0.cloudfront.net/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
# specifies the path to MathJax rendering script.
# You might want to use your own MathJax script to render formulas without
# Internet connection or if you want to use some special LaTeX packages.
# Note: path specified there cannot be an absolute path to a script on your hdd,
# instead you should run your (local if you wish) HTTP server which will
# serve the MathJax.js script. You can easily (in four lines of code) serve
# MathJax.js using http://happstack.com/docs/crashcourse/FileServing.html
# Do not forget the "http://" prefix (e.g. http://localhost:1234/MathJax.js)
The link to the tutorial is broken, so I'd be grateful for some assistance. Is there is any MathJax configuration I need to change, or simply extracting the files will do? I'll be writing lots of math in gitit. I'd prefer not to set up Apache etc. to serve MathJax. Gitit already uses Happstack, I'd prefer using that. Thanks!
EDIT: Just to be clear I'm not sure how to assign the port 1234 to serve this script
Ok I got MathJax working using portable Apache and the MathJax archive downloaded from docs.mathjax.org. The URL needs to be of the form (assuming you extracted the files into apache2/htdocs/MathJax):
http://localhost/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML
I wanted to keep this lightweight by reusing the same instance of Happstack as Gitit, but that seems beyond my skills/available time right now.
EDIT: Just found out that ghc will pack everything into one exe when building. So I doubt it is even possible to use the same Happstack instance, as the root directory of the server doesn't exist?
From the documentation, the static directory should work just fine:
On receiving a request, gitit always looks first in the static
directory (or in whatever directory is specified for static-dir in the
configuration file). If a file corresponding to the request is found
there, it is served immediately. If the file is not found in static,
gitit next looks in the static subdirectory of gitit's data file
($CABALDIR/share/gitit-x.y.z/data). This is where default css, images,
and javascripts are stored. If the file is not found there either,
gitit treats the request as a request for a wiki page or wiki command.
So, you can throw anything you want to be served statically (for
example, a robots.txt file or favicon.ico) in the static directory.
You can override any of gitit's default css, javascript, or image
files by putting a file with the same relative path in static. Note
that gitit has a default robots.txt file that excludes all URLs
beginning with /_.
(source: https://github.com/jgm/gitit)
Download the MathJax.js file from e.g. cdn.mathjax.org and place it in data/static/js/MathJax.js. Then change the config you quote to:
mathjax-script: http://localhost:5001/js/MathJax.js

Google tells do not change dynamic URLs, and offers this instead, how?

If you want to serve a static equivalent of your site, you might want to consider transforming the underlying content by serving a replacement which is truly static. One example would be to generate files for all the paths and make them accessible somewhere on your site.
What they mean exactly? And how to do it?
Your question: What do they mean exactly?
If you want to serve a static equivalent of your site - static refers to html pages that are not dynamically created.
you might want to consider transforming the underlying content by serving a replacement which is truly static. Have 'hard copies' of your pages with the different alternatives
One example would be to generate files for all the paths and make them accessible somewhere on your site. Go through your site and create static html pages (or pdf's) of each one and store them in the file structure that is represented by the URL.
Example of the last:
http://site.tld/product/pear which today is a dynamic (created on the fly by the code and database) but is not really in an actual folder on the server called product. They are suggesting to create a copy of the dynamically created page and store it in an actual folder on the server called product with the name pear.
Your question And how to do it?
Will that work - sort of if you wanted to by adding a .html to the physical file (copy of the dynamic one) and save it but I suspect you will run into all sorts of difficulties that you will need to overcome with the redirect code in places like .htaccess. Another option may be change the domain part of the URL to include static ie http://static.site.tld/ for the static copies and the original URL as is for the dynamic version.
The other big challenge then becomes maintaining the two copies because the concept they talk about is for the content (what is shown in the browser) to remain static over time. Kind of breaks the whole concept of how we build dynamic web sites today e.g. online shops etc.
For example if it's a shop, I would use PHP to also create the physical file when a product is added and not include parts that are going to change, rather include a link to the dynamic info something like:
<?php
$file = 'product/pear.html';
// mysql code here to extract the info and format ready for writing
$content = "<html><head><title>$title_from_db</title></head><body>$page_content_from_db</body></html>";
// Write the contents to the file
file_put_contents($file, $content);
?>

Must include files in nanoc always be in the layouts folder?

We would like to use server side includes (SSI) in the content of our nanoc site. This would allow us to chunk shared information and include it in multiple pages. It would also allow us to only maintain one source file of this shared information.
I figured out how to add an SSI (partial) in nanoc. As you know in nanoc we have the following root level folders, among others:
/content/
/layouts/
According to the instructions I found (https://nanoc.ws/doc/items-and-layouts/#partials), it seems the “partials” or include files need to be in the /layouts/ folder (outside of the /content/ folder).
For example:
The following code is used to insert the include file /layouts/partials/shared/test.html
<%= render 'partials/shared/test/' %>
In other words, the code assumes the include file will be placed in the layouts folder. Do you know of a way to change the default path for SSIs to /content/? This way we won’t mix content with layouts?
Thank you in advance.
Partials by default are in the layouts/ directory, and are used through the rendering helper using the #render helper method.
However, you can also put content to include (“partials”) in the content/ directory, too, although the approach then is different.
For example, create content/partials/foo.txt:
I am included!
Make sure the partial files are not routed nor compiled, so edit Rules and ensure these rules are executed first:
compile '/partials/*' do
write nil
end
Now in any item or layout you can include this partial:
<%= #items['/partials/foo.*'].compiled_content %>
This assumes the includer item/layout is filtered using ERB, but you could also use Haml or so.
You can let the compile rule do some processing, e.g. perform Markdown filtering, and then the HTML output from the filter will be included.

How to use .htaccess to reformat a directory listing while including the directory name?

For directory listings, I've created a common .htaccess file as well as a common header ("HeaderName /header.html).
Everything works fine, except one thing: while I don't want the derpy h1 version of "Index of /blah", I'd like to display a customized (and normal-sized) version of that, e.g., "CurDir = /blah". By default, specifying the HeaderName replaces all of that with some static text.
I tried embedding a bit of PHP (getcwd()) but that just returned the root directory where the header.html lives, no matter what subtree was being displayed. Surely there's some way to access the CWD without having to sprinkle control files like this in each subfolder?
I stumbled upon what may be a suitable environment variable while looking at the phpinfo() output... but is there a better (more common) way people handle this, perhaps without even using PHP?
<?php
$dir = getenv("REQUEST_URI");
?>

Best practice for white-labeling a static web site?

I have a directory structure with files under directory 'web', which represents a white-label web site (html files, images, js etc.).
I also have some twenty different 'brands' let's call them 'web-1', 'web-2' etc. each containing specific files that should override the files in 'web' for a specific brand.
Apache is configured to find the files for each virtual site i in document root 'website-i'.
In order for each 'website-i' to contain content like 'web' with the overrides for brand 'web-i', my build script first copies all of 'web' to 'website-i' and then overrides it with the source files from 'web-i'.
There several problems with this approach:
It takes time to copy the files.
It takes a lot of disk space
Adding a new brand requires adding to the script.
Is there a best practice for doing this in a way that does not require duplicating files?
(with Apache and Linux)
Well, the best solution is with pretty simple server-side code but I'm going to assume you've already rejected that, maybe because you haven't permission to run code on the server (although if your hacking the config then you probably do).
A solution just in config could be to make it serve from the default root but rewrite the url if the file exists in the proper dir ...
RewriteCond /web-1/a_file -f
RewriteRule ^(.*)$ /web/$1
But you'd have to do this for every file in every brand. You might be able to be more clever about it if the files are stored in a dir that's the same as the hostname but that's getting too complex for me.
Seriously, server-side code is the way to go on this...
Strangely enough, a work colleague was in pretty much exactly the same situation a couple of weeks ago and he eventually rewrote it as PHP. Each site is a row in a database, one page which pulls out the changed text and urls for images, etc. and falls back to a default if there's nothing there.
Having said all that. Using links, as you say above, solves problem 2, probably much of 1 and I don't think there's a way around 3 any way.

Resources