<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lateral Code &#187; htaccess</title>
	<atom:link href="http://www.lateralcode.com/tag/htaccess/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lateralcode.com</link>
	<description>A Web Development Blog Focused on Code and Technology</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Force Download Dialog Boxes</title>
		<link>http://www.lateralcode.com/force-download-box/</link>
		<comments>http://www.lateralcode.com/force-download-box/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 13:00:12 +0000</pubDate>
		<dc:creator>Patrick Lin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1434</guid>
		<description><![CDATA[When we browse the web, we click on links. That&#8217;s what makes the web a web &#8211; the links between pages. Sometimes, those links lead us not to pages, but to files. Many types of files pop up dialog boxes prompting downloads &#8211; executables, videos, etc. However, certain filetypes, thanks to modern web browsers, are [...]]]></description>
			<content:encoded><![CDATA[<p>When we browse the web, we click on links. That&#8217;s what makes the web a web &#8211; the links between pages. Sometimes, those links lead us not to pages, but to files. Many types of files pop up dialog boxes prompting downloads &#8211; executables, videos, etc.</p>
<p>However, certain filetypes, thanks to modern web browsers, are rendered as an in-browser page, such as images, PDFs, and MP3s. While this may be a convenience, at times we may wish to force the user to download the files, not just view them in their browser.</p>
<p>In this article I will show two easy ways to do this. The first is using .htaccess, and the second is using PHP.</p>
<p><span id="more-1434"></span></p>
<h2>.htaccess</h2>
<p>In your .htaccess file, add the following line for each extension you wish to force download:</p>
<pre><code>AddType application/octet-stream <i>extension</i>
</code></pre>
<p>For example, if I wanted to force PDFs and MP3s to download, I would do this:</p>
<pre><code>AddType application/octet-stream .pdf
AddType application/octet-stream .mp3
</code></pre>
<p>It&#8217;s really quite simple, and can be done quickly.</p>
<h2>PHP</h2>
<p>The PHP version is a bit more complicated, but can be useful if you want to have a bit more control. For example, you can use download IDs instead of the filename to prevent direct hotlinking. For example, you can assign the ID &#8220;3148&#8243; to &#8220;BusinessPlan.pdf&#8221; and do a forced download.</p>
<p>The basic code looks like this:</p>
<pre><code>&lt;?php
header('Content-Disposition:inline;filename="'.$file.'"');
header('Content-Transfer-Encoding:Binary');
header('Content-length:'.filesize($file));
header('Content-Type:application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile("$file");
?&gt;
</code></pre>
<p>Of course, this is a huge security hole if you use the structure <code>download.php?file=BusinessPlan.pdf</code>, as anyone can access any file on your webserver. Thus, steps should be taken to secure the user input, or use a database of IDs.</p>
<p>Of course, if you&#8217;re just forcing download and don&#8217;t need extra control over the download process, you should use the .htaccess version, as it is much simpler and easier to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/force-download-box/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Need more organization? Make a template-focused site! &#8211; Part 1</title>
		<link>http://www.lateralcode.com/template-focused-site1/</link>
		<comments>http://www.lateralcode.com/template-focused-site1/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 12:00:39 +0000</pubDate>
		<dc:creator>Karthik Viswanathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=1297</guid>
		<description><![CDATA[Have you ever had to deal with a sloppy site? Maybe it was when you were working with a client. Or could it be your own site, which you formatted poorly because of inexperience? Regardless, there is a simple yet efficient way to make your site based completely off a template. All it requires is [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever had to deal with a sloppy site? Maybe it was when you were working with a client. Or could it be your own site, which you formatted poorly because of inexperience? Regardless, there is a simple yet efficient way to make your site based completely off a template. All it requires is a bit of htaccess and some PHP.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/template-focused-site1/2.jpg" alt="Unorganized" class="list-post-img"/></p>
<h2>Time for some incentive</h2>
<p>This will be a multi-part tutorial in which we explain how to completely transform your site into an organized beast. Today&#8217;s section will focus on a few lines of .htaccess that you will soon come to love. But, before we begin, let&#8217;s go over a few  advantages and some incentive for creating a template-focused site.</p>
<p><span id="more-1297"></span></p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/template-focused-site1/3.jpg" alt="Advantages" class="list-post-img"/></p>
<ol>
<li>Maintained through a simple template that regulates code and prevents reuse. This enables you to focus on content rather than code.</li>
<li>Easily editable by changing any part of the template. Doing so will immediately change all pages of a site. There is no need to go into multiple files.</li>
<li>Can simply be hooked up with a MySQL database for even further organization.</li>
<li>Adding pages that jive with your site is as simple as making a file with some text (not HTML/PHP&#8211;just content)</li>
<li>Can easily be created to have pretty URLs without having to delve deep into .htaccess.</li>
</ol>
<p>The above list just constitutes a small number of reasons to switch to a template-focused site. A quick note for all you CMS users (WordPress/Joomla/Drupal/etc.): your site is already optimized with a template! This tutorial is mostly referring to those who are creating or have a site that is not running on a CMS.</p>
<h2>Bring on the htaccess!</h2>
<p>Do you need to be a htaccess genius to create a template-focused site? Of course not! Take a look at the following code. This is all you&#8217;ll need:</p>
<pre><code>RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cur=$1 [L,QSA]</code></pre>
<p>It may look a bit complicated at first, but it is actually quite simple. This all starts with a request to your site (such as <code>"http://your-site.com/something.html"</code>). If the given request is not a file nor a directory, it will redirect the request to index.php. </p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/template-focused-site1/4.jpg" alt="Request" class="list-post-img"/></p>
<p>As an example, if the user requested <code>"http://your-site.com/asdf"</code>, which is neither a file nor a directory, it will redirect to <code>"http://your-site.com/index.php?cur=asdf"</code>. The great part about this is that the URL will stay the same. The user will still see <code>"http://your-site.com/asdf"</code> in their URL bar, but index.php will actually be handling the request. Not only does this give you great flexibility, but it makes the management of failed requests (such as not found, forbidden, etc.) extremely simple.</p>
<h2>How about that PHP?</h2>
<p>Consider the following index.php:</p>
<pre><code>&lt;?php
	$request = isset( $_GET[ 'cur' ] ) ? $_GET[ 'cur' ] : 'home';

	$pathToFile = dirname( __FILE__ ) . '/pages/';
	$fullPath = $pathToFile . $request . '.php';

	if( file_exists( $fullPath ) )
		require_once $fullPath;
	else
		require_once $pathToFile . '404.php';
?&gt;</code></pre>
<p>This is used to parse the request from the user. It first finds the requested page by getting the cur parameter passed to index.php (which is a GET request). If this doesn&#8217;t exist, it defaults to the home page. Next, it finds the corresponding PHP page under the pages/ directory and then includes it. So, for example, let&#8217;s say you had the following setup:</p>
<pre><code>pages/
.htaccess
index.php</code></pre>
<p>And in the pages/ directory, you have:</p>
<pre><code>404.php
home.php
page.php</code></pre>
<p>If cur is not set, index.php will display home.php in the pages/ directory. Otherwise, it&#8217;ll display [cur].php in the pages/ directory, where [cur] is the value of cur.</p>
<h2>Sample Occurrences</h2>
<p>So, let&#8217;s trace a few requests for this site. If the user goes to <code>"http://your-site.com"</code>, the htaccess will redirect the user to <code>"http://your-site.com/index.php?cur="</code>.</p>
<p>Next, index.php will receive the request. It will realize cur is not explicitly defined, so it will set it to &#8220;home&#8221;. After that, it will find the full path to home.php in the pages directory and make sure that the file exists. Because this file does exist, it will include it into the page and the contents will be displayed.</p>
<p>How about <code>"http://your-site.com/page"</code>? This will be redirected to <code>"http://your-site.com/index.php?cur=page"</code>.</p>
<p>Since cur is set, index.php will find the corresponding file (which is page.php) inside the pages directory. Once again, because this file exists, it will be included into the page.</p>
<p>Let&#8217;s do one last example. Consider <code>"http://your-site.com/test"</code>. This will redirect internally to <code>"http://your-site.com/index.php?cur=test"</code>.</p>
<p>Cur is explicitly defined. Thus, index.php will look for test.php in the pages/ directory. Unfortunately, this file does not exist. Thus, index.php will serve up 404.php in the pages/ directory. This file displays information that tells the user a 404 (not found) error occurred.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/template-focused-site1/5.jpg" alt="404 Error" class="list-post-img"/></p>
<p>To see this all in action, you may view a demo of the site setup by clicking the link below. Please note that there is no real content on the site &#8211; it is just an example that tells you what page is being delivered. Consider requesting &#8220;/&#8221;, &#8220;/page&#8221;, and &#8220;/test&#8221; to see the home.php, page.php, and 404.php in action, respectively.</p>
<p><a href="http://demo.lateralcode.com/template-focused-site1/" class="view">View a Demo</a> <a href="/wp-content/uploads2/template-focused-site1/template-focused-site1.zip" class="dl">Download the Files</a></p>
<p>As you can see, with just a little amount of code, the effects of templating can be great. This, of course, is just the starting line of a template-focused site. There is still a lot more to cover. Join us in the next installment when we expand on this concept and teach you how to become even more organized.</p>
<p><img src="http://www.lateralcode.com/wp-content/uploads2/template-focused-site1/1.jpg" alt="Organized" class="list-post-img"/></p>
<p>If you liked this post, please leave an encouraging comment below! It really helps us keep Lateral Code up and running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/template-focused-site1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GZip files with .htaccess and PHP</title>
		<link>http://www.lateralcode.com/gzip-files-with-htaccess-and-php/</link>
		<comments>http://www.lateralcode.com/gzip-files-with-htaccess-and-php/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 07:13:07 +0000</pubDate>
		<dc:creator>Patrick Lin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.lateralcode.com/?p=32</guid>
		<description><![CDATA[Many hosts have a set bandwidth clients can use. In this day and age, files are getting larger and heavier, but bandwidth costs aren&#8217;t getting much cheaper. So, one of the best and easiest things to do is to GZip. From &#8220;The Definitive Post on GZipping your CSS&#8221; CSS files for larger sites can become [...]]]></description>
			<content:encoded><![CDATA[<p>Many hosts have a set bandwidth clients can use. In this day and age, files are getting larger and heavier, but bandwidth costs aren&#8217;t getting much cheaper. So, one of the best and easiest things to do is to GZip.</p>
<p>From &#8220;<a title="The Definitive Post on GZipping your CSS" href="http://www.fiftyfoureleven.com/weblog/web-development/css/the-definitive-css-gzip-method">The Definitive Post on GZipping your CSS</a>&#8221;</p>
<blockquote><p>CSS files for larger sites can become pretty large themselves. Gzipping or compressing these files has shown to provide a reduction in the neighborhood of 70-80% of the original file size, a fairly significant &#8216;weight loss&#8217;.</p></blockquote>
<p>So obviously, GZipping CSS is great. But what about JS? JavaScript files are becoming increasingly huge, so what should we do about that?</p>
<p><span id="more-32"></span></p>
<p>The article from Fiftyfoureleven that is linked to suggests using the following PHP snippet:</p>
<pre><code>&lt;?php
    ob_start ("ob_gzhandler");
    header("Content-type: text/css; charset: UTF-8");
    header("Cache-Control: must-revalidate");
    $offset = 60 * 60 ;
    $ExpStr = "Expires: " .
    gmdate("D, d M Y H:i:s",
    time() + $offset) . " GMT";
    header($ExpStr);
?&gt;</code></pre>
<p>and then the following .htaccess snippet:</p>
<pre><code>AddHandler application/x-httpd-php .css
php_value auto_prepend_file gzip-css.php
php_flag zlib.output_compression On</code></pre>
<p>So then the obvious solution for JS files would be to make a file called gzip-js.php, with the same PHP snippet with the content type modified to text/javascript, like the one seen at <a href="http://perishablepress.com/press/2007/04/24/compressed-javascript-compression/">Perishable Press</a>.</p>
<p>Then we run into a problem. For systems like <a title="Wordpress" href="http://wordpress.org/">WordPress</a> or <a title="Expression Engine" href="http://expressionengine.com/">ExpressionEngine</a> or <a title="Habari" href="http://www.habariproject.org/en/">Habari</a>, where css or js files might be sent from many different folders, manually putting the .htaccess and gzip file may not be convenient, so I created this system.</p>
<p>gzip.php:</p>
<pre class="php">&lt;?php
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) &amp;&amp; substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start('ob_gzhandler');
else
ob_start();
?&gt;</code></pre>
<p>.htaccess:</p>
<pre><code>################ Expires Control ################
ExpiresActive On
ExpiresDefault A0
&lt;FilesMatch "\.(gif|jpg|jpeg|png|swf)$"&gt;
# 2 weeks
ExpiresDefault A1209600
Header append Cache-Control "public"
&lt;/FilesMatch&gt;
&lt;FilesMatch "\.(xml|txt|html)$"&gt;
# 2 hours
ExpiresDefault A7200
Header append Cache-Control "proxy-revalidate"
&lt;/FilesMatch&gt;
&lt;FilesMatch "\.(js|css)$"&gt;
# 3 days
ExpiresDefault A259200
Header append Cache-Control "proxy-revalidate"
&lt;/FilesMatch&gt;

################## GZip Files ###################
&lt;FilesMatch "\.js$"&gt;
AddHandler application/x-httpd-php .js
php_value default_mimetype "text/javascript"
&lt;/FilesMatch&gt;
&lt;FilesMatch "\.css$"&gt;
AddHandler application/x-httpd-php .css
php_value default_mimetype "text/css"
&lt;/FilesMatch&gt;
&lt;FilesMatch "\.(htm|html|shtml)$"&gt;
AddHandler application/x-httpd-php .html
php_value default_mimetype "text/html"
&lt;/FilesMatch&gt;
php_value auto_prepend_file /absolute/path/to/gzip.php</code></pre>
<p>This snippet allows me to control the expires and content type using htaccess instead of PHP, like in the other examples. And because I use the absolute path to gzip.php, I can ensure that GZip gets applied to all php, js, css, html, shtml, and htm files.</p>
<dl>
<dt>Sources</dt>
<dd>
<ul>
<li><a title="The Definitive Post on GZipping your CSS" href="http://www.fiftyfoureleven.com/weblog/web-development/css/the-definitive-css-gzip-method">The Definitive Post on GZipping your CSS</a> &#8211; Fiftyfoureleven</li>
<li><a title="Compressed CSS Compression" href="http://perishablepress.com/press/2006/10/23/compressed-css-compression/">Compressed CSS Compression</a> &#8211; Perishable Press</li>
<li><a title="Compressed JavaScript Compression" href="http://perishablepress.com/press/2007/04/24/compressed-javascript-compression/">Compressed JavaScript Compression</a> &#8211; Perishable Press</li>
</ul>
</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://www.lateralcode.com/gzip-files-with-htaccess-and-php/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
