If you have a shared Linux hosting package through 1&1, you have no doubt discovered that the usual method of changing your .htaccess file does not allow you to compress your files using Apache because 1&1 has disabled the mod_deflate and mod_gzip modules; you also cannot specify php values in .htaccess files in 1&1 shared packages, so a solution like this one also will not work. However, there is a solution that does work, and it works even on the most basic 1&1 Beginner’s Package.
Note: If you plan to implement the Minify! option I blog about in How to Easily Combine, Minify, and Cache JavaScript and CSS..., you can safely skip steps 1 and 2 and include only the first line shown in step 3.
Step 1:
Determine what your root path is; if you are using a 1&1 package, it will look something like this:/kunden/homepages/12/d123456789/htdocs/You can do that by creating a text file in Notepad or other editor,* entering the following lines, then saving it with as phpinfo (or whatever you want) followed by the .php extension.
<?php phpinfo(); ?>
(* If you’re using Notepad, you’ll probably want to put quotation marks around the name "phpinfo.php" when saving, to keep it from saving the file as phpinfo.php.txt.
Personally, after passing from Geany on Windows (used Notepad++ some), and Komodo Edit on Mac OSX, my editor of choice is now a customized version of vim. Incidentally, the three previously mentioned IDEs mutually rely on the Scintilla editor as their base.)
Personally, after passing from Geany on Windows (used Notepad++ some), and Komodo Edit on Mac OSX, my editor of choice is now a customized version of vim. Incidentally, the three previously mentioned IDEs mutually rely on the Scintilla editor as their base.)
Now browse to the newly uploaded file with your web browser and search for DOCUMENT_ROOT. This variable will tell you what the exact path is to your server.
Step 2:
Now create another php file in the same way, this time adding the following information so that when you compress your CSS and JS files, they are served with the right headers, or else they will not load properly in Firefox and other browsers:<?php if (isset($_SERVER['SCRIPT_FILENAME'])) { $timestamp = filemtime(__FILE__); header('Last-Modified: ' . $timestamp); $expires = 60*60*24*14; header("Pragma: public"); header("Cache-Control: maxage=".$expires); header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); header('Vary: Accept-Encoding'); $pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']); $extension = $pathinfo['extension']; if ($extension == 'css') { header('Content-type: text/css'); } if ($extension == 'js') { header('Content-type: text/javascript'); } }?>Make sure that you don’t space down between the lines or add an extra space at the end, and save this file as headers.php, uploading it to wherever you keep your scripts. For the sake of this example, I will assume that you store your scripts in a directory named webscripts.
[Update: As Brett reminds me in the comment below, you have now given PHP control of rendering your pages, so you will also need to include in this file any other header information you know you’ll need by declaring each header on its own line. For example, notice this extra line:
header('Vary: Accept Encoding');
Based on the readership of these articles, this will be particularly useful when optimizing for speed using a free tool like Google's Page Speed and the Yahoo! based YSlow. Add as many extra headers as you need, one per line.]
header('Vary: Accept Encoding');
Based on the readership of these articles, this will be particularly useful when optimizing for speed using a free tool like Google's Page Speed and the Yahoo! based YSlow. Add as many extra headers as you need, one per line.]
Step 3:
Create a third file, this time a PHP configuration file, entering the following information (though of course changed with the information you looked up in step 1 and whatever directory you use for step 2—unfortunately, you can’t use environmental variables in .ini files, as they are straight ASCII):zlib.output_compression=1 auto_prepend_file=/kunden/homepages/12/d123456789/htdocs/webscripts/headers.phpName your configuration file the conventional php.ini and save it. (If for some reason zlib.output_compression=1 doesn't work, try output_handler=ob_gzhandler instead.)
The first line tells the server to use PHP’s built-in g-zip handler for compression; the second line points to the exact location of the file you created in step 2: /kunden/homepages/12/d123456789/htdocs/ is the document root you located in step 1, and webscripts/ is the directory where you uploaded the headers.php script.
auto_prepend_file tells the server to automatically “prepend,” or “attach to the beginning of” all pages it serves (versus auto_append_file, which would “append,” or “attach to the end” of all files). The if/then conditional statements in step 2 ensures that the headers.php file will only attach headers if the file in question has a .css or .js extension.
Note: Check with your web host. With 1&1, at least, you will need to upload a copy of your php.ini file to EVERY directory containing files you want compressed.
Step 4:
Create an .htaccess file or modify your existing one by adding the following line (if you’re not familiar with an .htaccess file, see here):CAUTION: If you plan to implement the Minify! solution I blog about in How to Easily Combine, Minify, and Cache JavaScript and CSS..., I do not recommend including the .css and .js file extensions on this line. Doing so processes these files twice, as Minify! already has its own mechanism for compressing files. And, as a result of double compression, certain versions of Internet Explorer choke and die silently.
AddType x-mapp-php5 .php .shtml .html .htm .txt .js .cssThis line tells the server to process all the file extensions specified through PHP5; change it to x-mapp-php4 if you want to use PHP4 instead. You can add other extensions or take away from the ones here: whatever extensions are listed on the line after the x-mapp-php5 will be processed via PHP.
Note: On other Linux hosts, you may need to use php5-script instead of x-mapp-php5.
[June 4, 2011 Update: I was setting up my sister's site recently, also on a 1&1 Beginner’s Package, and the style sheets were not working correctly. The fix was to add this line to the .htaccess file immediately above the line shown in the example above:
RemoveHandler .cssI first tried removing all file extensions I was using, including .htm, but this caused problems. Just know that you have the option of removing file handlers and play around with it as needed.