<br />
<b>Warning</b>:  Illegal offset type in isset or empty in <b>/home/antihostmonster/www.antihostmonster.com/wp-includes/post.php</b> on line <b>828</b><br />
<br />
<b>Warning</b>:  Illegal offset type in isset or empty in <b>/home/antihostmonster/www.antihostmonster.com/wp-includes/post.php</b> on line <b>828</b><br />
<?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>Anti HostMonster &#187; avoid</title>
	<atom:link href="http://www.antihostmonster.com/tag/avoid/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.antihostmonster.com</link>
	<description>Do not fool us</description>
	<lastBuildDate>Mon, 23 Jan 2012 12:42:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Basic vi Commands</title>
		<link>http://www.antihostmonster.com/2011/07/basic-vi-commands/</link>
		<comments>http://www.antihostmonster.com/2011/07/basic-vi-commands/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 02:26:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SSH Commands]]></category>
		<category><![CDATA[avoid]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tar]]></category>
		<category><![CDATA[vi editor]]></category>

		<guid isPermaLink="false">http://www.antihostmonster.com/?p=219</guid>
		<description><![CDATA[




Basic vi Commands






What is vi?

The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.] 
The UNIX vi editor is a full screen editor and has two modes of  [...]]]></description>
			<content:encoded><![CDATA[<table width="98%">
<tbody>
<tr>
<td align="LEFT"></td>
<td align="RIGHT">
<h1 align="RIGHT"><span style="color: navy;">Basic vi Commands</span></h1>
</td>
</tr>
</tbody>
</table>
<hr noshade="noshade" />
<hr noshade="noshade" />
<h3>What is <tt>vi</tt>?</h3>
<dl>
<dd>The default editor that comes with the UNIX operating system is called <tt>vi</tt> (<strong>vi</strong>sual editor). [Alternate editors for UNIX environments include <tt>pico</tt> and <tt>emacs</tt>, a product of GNU.] </dd>
<dd>The UNIX <tt>vi</tt> editor is a full screen editor and has two modes of operation:</p>
<ol>
<li><em>Command mode</em> commands which cause action to be taken on the file, and</li>
<li><em>Insert mode</em> in which entered text is inserted into the file.</li>
</ol>
<p>In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the <tt>vi</tt> editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <tt>&lt;Esc&gt;</tt> (<em>Escape</em>) key turns off the Insert mode.</p>
</dd>
<dd>While there are a number of <tt>vi</tt> commands, just a handful of these is usually sufficient for beginning <tt>vi</tt> users. To assist such users, this Web page contains a sampling of basic <tt>vi</tt> commands. The most basic and useful commands are marked with an asterisk (<tt>*</tt> or star) in the tables below. With practice, these commands should become automatic. </dd>
<dd><strong>NOTE:</strong> Both UNIX and <tt>vi</tt> are <strong>case-sensitive</strong>. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.</dd>
</dl>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>To Get Into and Out Of <tt>vi</tt></h3>
<p>&nbsp;</p>
<h4>To Start <tt>vi</tt></h4>
<p>&nbsp;</p>
<dl>
<dd>To use <tt>vi</tt> on a file, type in <tt>vi filename</tt>. If the file named <tt>filename</tt> exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.</dd>
</dl>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT"><tt>vi filename</tt></th>
<td><em>edit <tt>filename</tt> starting at line 1</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>vi -r filename</tt></th>
<td><em>recover <tt>filename</tt> that was being edited when system crashed</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>To Exit <tt>vi</tt></h4>
<p>&nbsp;</p>
<dl>
<dd>Usually the new or modified file is saved when you leave <tt>vi</tt>. However, it is also possible to quit <tt>vi</tt> without saving the file. </dd>
<dd><strong>Note:</strong> The cursor moves to bottom of screen whenever a colon (<tt>:</tt>) is typed. This type of command is completed by hitting the <tt>&lt;Return&gt;</tt> (or <tt>&lt;Enter&gt;</tt>) key.</dd>
</dl>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT"><tt>: x</tt><tt>&lt;Return&gt;</tt></th>
<td><em>quit <tt>vi</tt>, writing out modified file to file named in original invocation</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>:wq</tt><tt>&lt;Return&gt;</tt></th>
<td><em>quit <tt>vi</tt>, writing out modified file to file named in original invocation</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>:q</tt><tt>&lt;Return&gt;</tt></th>
<td><em>quit (or exit) <tt>vi</tt></em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT"><tt>:q!</tt><tt>&lt;Return&gt;</tt></th>
<td><em>quit <tt>vi</tt> even though latest changes have not been saved for this <tt>vi</tt> call</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>Moving the Cursor</h3>
<p>&nbsp;</p>
<dl>
<dd>Unlike many of the PC and MacIntosh editors, <strong>the mouse does not move the cursor</strong> within the <tt>vi</tt> editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since <tt>vi</tt> was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in <tt>vi</tt> and should be avoided. </dd>
<dd>If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two. </dd>
<dd>In the table below, the symbol <tt>^</tt> before a letter means that the <tt>&lt;Ctrl&gt;</tt> key should be held down while the letter key is pressed.</dd>
</dl>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT"><tt>j</tt> <em>or</em> <tt>&lt;Return&gt;</tt><br />
[<em>or</em> down-arrow]</th>
<td><em>move cursor down one line</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT"><tt>k</tt> [<em>or</em> up-arrow]</th>
<td><em>move cursor up one line</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT"><tt>h</tt> <em>or</em> <tt>&lt;Backspace&gt;</tt><br />
[<em>or</em> left-arrow]</th>
<td><em>move cursor left one character</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT"><tt>l</tt> <em>or</em> <tt>&lt;Space&gt;</tt><br />
[<em>or</em> right-arrow]</th>
<td><em>move cursor right one character</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>0</tt> (zero)</th>
<td><em>move cursor to start of current line (the one with the cursor)</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>$</tt></th>
<td><em>move cursor to end of current line</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>w</tt></th>
<td><em>move cursor to beginning of next word</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>b</tt></th>
<td><em>move cursor back to beginning of preceding word</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:0</tt><tt>&lt;Return&gt; <em>or</em> 1G</tt></th>
<td><em>move cursor to first line in file</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:n</tt><tt>&lt;Return&gt; <em>or</em> nG</tt></th>
<td><em>move cursor to line <tt>n</tt></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>:$</tt><tt>&lt;Return&gt; <em>or</em> G</tt></th>
<td><em>move cursor to last line in file</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>Screen Manipulation</h3>
<p>&nbsp;</p>
<dl>
<dd>The following commands allow the <tt>vi</tt> editor screen (or window) to move up or down several lines and to be refreshed.</dd>
</dl>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^f</tt></th>
<td><em>move forward one screen</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^b</tt></th>
<td><em>move backward one screen</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^d</tt></th>
<td><em>move down (forward) one half screen</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^u</tt></th>
<td><em>move up (back) one half screen</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^l</tt></th>
<td><em>redraws the screen</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^r</tt></th>
<td><em>redraws the screen, removing deleted lines</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>Adding, Changing, and Deleting Text</h3>
<p>&nbsp;</p>
<dl>
<dd>Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables. </dd>
<dd>Perhaps the most important command is the one that allows you to back up and <em>undo</em> your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.</dd>
</dl>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>u</tt></th>
<td><em>UNDO WHATEVER YOU JUST DID; a simple toggle</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<dl>
<dd>The main purpose of an editor is to create, add, or modify text for a file. </dd>
</dl>
<h4>Inserting or Adding Text</h4>
<p>&nbsp;</p>
<dl>
<dd>The following commands allow you to insert and add text. Each of these commands puts the <tt>vi</tt> editor into insert mode; thus, the <tt>&lt;Esc&gt;</tt> key must be pressed to terminate the entry of text and to put the <tt>vi</tt> editor back into command mode. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>i</tt></th>
<td><em>insert text before cursor, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>I</tt></th>
<td><em>insert text at beginning of current line, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>a</tt></th>
<td><em>append text after cursor, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>A</tt></th>
<td><em>append text to end of current line, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>o</tt></th>
<td><em>open and put text in a new line below current line, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>O</tt></th>
<td><em>open and put text in a new line above current line, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Changing Text</h4>
<p>&nbsp;</p>
<dl>
<dd>The following commands allow you to modify text. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>r</tt></th>
<td><em>replace single character under cursor (no <tt>&lt;Esc&gt;</tt> needed)</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>R</tt></th>
<td><em>replace characters, starting with current cursor position, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>cw</tt></th>
<td><em>change the current word with new text,<br />
starting with the character under cursor, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>cNw</tt></th>
<td><em>change <tt>N</tt> words beginning with character under cursor, until <tt>&lt;Esc&gt;</tt> hit;<br />
e.g., <tt>c5w</tt> changes 5 words</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>C</tt></th>
<td><em>change (replace) the characters in the current line, until <tt>&lt;Esc&gt;</tt> hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>cc</tt></th>
<td><em>change (replace) the entire current line, stopping when <tt>&lt;Esc&gt;</tt> is hit</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>Ncc</tt> <em>or</em> <tt>cNc</tt></th>
<td><em>change (replace) the next N lines, starting with the current line,<br />
stopping when <tt>&lt;Esc&gt;</tt> is hit</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Deleting Text</h4>
<p>&nbsp;</p>
<dl>
<dd>The following commands allow you to delete text. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>x</tt></th>
<td><em>delete single character under cursor</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>Nx</tt></th>
<td><em>delete N characters, starting with character under cursor</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>dw</tt></th>
<td><em>delete the single word beginning with character under cursor</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>dNw</tt></th>
<td><em>delete <tt>N</tt> words beginning with character under cursor;<br />
e.g., <tt>d5w</tt> deletes 5 words</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>D</tt></th>
<td><em>delete the remainder of the line, starting with current cursor position</em></td>
</tr>
<tr>
<th>*</th>
<th align="LEFT" nowrap="nowrap"><tt>dd</tt></th>
<td><em>delete entire current line</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>Ndd</tt> <em>or</em> <tt>dNd</tt></th>
<td><em>delete <tt>N</tt> lines, beginning with the current line;<br />
e.g., <tt>5dd</tt> deletes 5 lines</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Cutting and Pasting Text</h4>
<p>&nbsp;</p>
<dl>
<dd>The following commands allow you to copy and paste text. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>yy</tt></th>
<td><em>copy (yank, cut) the current line into the buffer</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>Nyy</tt> <em>or</em> <tt>yNy</tt></th>
<td><em>copy (yank, cut) the next N lines, including the current line, into the buffer</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>p</tt></th>
<td><em>put (paste) the line(s) in the buffer into the text after the current line</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>Other Commands</h3>
<p>&nbsp;</p>
<h4>Searching Text</h4>
<p>&nbsp;</p>
<dl>
<dd>A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th></th>
<th align="LEFT"><tt>/string</tt></th>
<td><em>search forward for occurrence of <tt>string</tt> in text<em></em></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>?string</tt></th>
<td><em>search backward for occurrence of <tt>string</tt> in text<em></em></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>n</tt></th>
<td><em>move to next occurrence of search string</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>N</tt></th>
<td><em>move to next occurrence of search string in opposite direction</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h4>Determining Line Numbers</h4>
<p>&nbsp;</p>
<dl>
<dd>Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful. </dd>
</dl>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th></th>
<th align="LEFT"><tt>:.=</tt></th>
<td><em>returns line number of current line at bottom of screen<em></em></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT"><tt>:=</tt></th>
<td><em>returns the total number of lines at bottom of screen<em></em></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>^g</tt></th>
<td><em>provides the current line number, along with the total number of lines,<br />
in the file at the bottom of the screen</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<p>&nbsp;</p>
<h3>Saving and Reading Files</h3>
<p>These commands permit you to input and output files other than the named file with which you are currently working.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="">
<tbody>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:r filename</tt><tt>&lt;Return&gt;</tt></th>
<td><em>read file named <tt>filename</tt> and insert after current line<br />
(the line with cursor)</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:w</tt><tt>&lt;Return&gt;</tt></th>
<td><em>write current contents to file named in original <tt>vi</tt> call</em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:w newfile</tt><tt>&lt;Return&gt;</tt></th>
<td><em>write current contents to a new file named <tt>newfile</tt></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:12,35w smallfile</tt><tt>&lt;Return&gt;</tt></th>
<td><em>write the contents of the lines numbered 12 through 35 to a new file named <tt>smallfile</tt></em></td>
</tr>
<tr>
<th></th>
<th align="LEFT" nowrap="nowrap"><tt>:w! prevfile</tt><tt>&lt;Return&gt;</tt></th>
<td><em>write current contents over a pre-existing file named <tt>prevfile</tt></em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<hr noshade="noshade" />
<hr noshade="noshade" />
<p><span style="font-size: xx-small;"><strong>Source cs.colostate.edu</strong></span></p>
<hr noshade="noshade" />
]]></content:encoded>
			<wfw:commentRss>http://www.antihostmonster.com/2011/07/basic-vi-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.htaccess Guide</title>
		<link>http://www.antihostmonster.com/2009/06/htaccess-guide/</link>
		<comments>http://www.antihostmonster.com/2009/06/htaccess-guide/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 11:37:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SSH Commands]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[avoid]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[ModRewrite]]></category>
		<category><![CDATA[Repair]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tar]]></category>

		<guid isPermaLink="false">http://www.antihostmonster.com/?p=110</guid>
		<description><![CDATA[General Information [ ^ ]
.htaccess Definition1 [ ^ ]
Apache server software provides distributed (i.e., directory-level) configuration via Hypertext Access files. These .htaccess files enable the localized fine-tuning of Apache’s universal system-configuration directives, which are defined in  [...]]]></description>
			<content:encoded><![CDATA[<h3 id="general">General Information <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>.</strong><strong>htaccess Definition</strong><sup>1</sup> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Apache server software provides distributed (i.e., directory-level) configuration via <em>Hypertext Access</em> files. These <a title="htaccess at Wikipedia" rel="nofollow" href="http://en.wikipedia.org/wiki/Htaccess"><code>.</code><code>htaccess</code></a> files enable the localized fine-tuning of Apache’s universal system-configuration directives, which are defined in Apache’s main configuration file. The localized <code>.</code><code>htaccess</code> directives must operate from within a file named <code>.</code><code>htaccess</code>. The user must have appropriate file permissions to access and/or edit the <code>.</code><code>htaccess</code> file. Further, <code>.</code><code>htaccess</code> file permissions should never allow world write access — a secure permissions setting is “644”, which allows universal read access and user-only write access. Finally, <code>.</code><code>htaccess</code> rules apply to the parent directory and all subdirectories. Thus to apply configuration rules to an entire website, place the <code>.</code><code>htaccess</code> file in the root directory of the site.</p>
<p><strong>Commenting .</strong><strong>htaccess Code</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Comments are essential to maintaining control over any involved portion of code. Comments in <code>.</code><code>htaccess</code> code are fashioned on a per-line basis, with each line of comments beginning with a pound sign <code>#</code>. Thus, comments spanning multiple lines in the <code>.</code><code>htaccess</code> file require multiple pound signs. Further, due to the extremely volatile nature of htaccess voodoo, it is wise to include only alphanumeric characters (and perhaps a few dashes and underscores) in any <code>.</code><code>htaccess</code> comments.</p>
<p><span id="more-110"></span></p>
<p><strong>Important Notes for .</strong><strong>htaccess Noobs</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>As a configuration file, <code>.</code><code>htaccess</code> is very powerful. Even the slightest syntax error (like a missing space) can result in severe server malfunction. Thus it is crucial to make backup copies of <em>everything</em> related to your site (including any original <code>.</code><code>htaccess</code> files) <em>before</em> working with your Hypertext Access file(s). It is also important to check your entire website thoroughly after making any changes to your <code>.</code><code>htaccess</code> file. If any errors or other problems are encountered, employ your backups immediately to restore original functionality.</p>
<p><strong>Performance Issues</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code>.</code><code>htaccess</code> directives provide directory-level configuration without requiring access to Apache’s main server cofiguration file (httpd.conf). However, due to performance and security concerns, the main configuration file should always be used for server directives whenever possible. For example, when a server is configured to process <code>.</code><code>htaccess</code> directives, Apache must search every directory within the domain and load any and all <code>.</code><code>htaccess</code> files upon every document request. This results in increased page processing time and thus decreases performance. Such a performance hit may be unnoticeable for sites with light traffic, but becomes a more serious issue for more popular websites. Therefore, <code>.</code><code>htaccess</code> files should only be used when the main server configuration file is inaccessible. See the “<a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#performance">Performance Tricks</a>” section of this article for more information.</p>
<p><strong>Regex Character Definitions for htaccess</strong><sup>2</sup> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<dl class="defs">
<dt><code>#</code></dt>
<dd>the <code>#</code> instructs the server to ignore the line. used for including comments. each line of comments requires it’s own <code>#</code>. when including comments, it is good practice to use only letters, numbers, dashes, and underscores. this practice will help eliminate/avoid potential server parsing errors.</dd>
<dt><code>[F]</code></dt>
<dd>Forbidden: instructs the server to return a <code>403 Forbidden</code> to the client.</dd>
<dt><code>[L]</code></dt>
<dd>Last rule: instructs the server to stop rewriting after the preceding directive is processed.</dd>
<dt><code>[N]</code></dt>
<dd>Next: instructs Apache to rerun the rewrite rule until all rewriting directives have been achieved.</dd>
<dt><code>[G]</code></dt>
<dd>Gone: instructs the server to deliver <code>Gone (no longer exists)</code> status message.</dd>
<dt><code>[P]</code></dt>
<dd>Proxy: instructs server to handle requests by <code>mod_proxy</code></dd>
<dt><code>[C]</code></dt>
<dd>Chain: instructs server to chain the current rule with the previous rule.</dd>
<dt><code>[R]</code></dt>
<dd>Redirect: instructs Apache to issue a redirect, causing the browser to request the rewritten/modified URL.</dd>
<dt><code>[NC]</code></dt>
<dd>No Case: defines any associated argument as case-<em>in</em>sensitive. i.e., &#8220;NC&#8221; = &#8220;No Case&#8221;.</dd>
<dt><code>[PT]</code></dt>
<dd>Pass Through: instructs <code>mod_rewrite</code> to pass the rewritten URL back to Apache for further processing.</dd>
<dt><code>[OR]</code></dt>
<dd>Or: specifies a logical &#8220;or&#8221; that ties two expressions together such that either one proving true will cause the associated rule to be applied.</dd>
<dt><code>[NE]</code></dt>
<dd>No Escape: instructs the server to parse output without escaping characters.</dd>
<dt><code>[NS]</code></dt>
<dd>No Subrequest: instructs the server to skip the directive if internal sub-request.</dd>
<dt><code>[QSA]</code></dt>
<dd>Append Query String: directs server to add the query string to the end of the expression (URL).</dd>
<dt><code>[S=x]</code></dt>
<dd>Skip: instructs the server to skip the next &#8220;x&#8221; number of rules if a match is detected.</dd>
<dt><code>[E=variable:value]</code></dt>
<dd>Environmental Variable: instructs the server to set the environmental variable &#8220;variable&#8221; to &#8220;value&#8221;.</dd>
<dt><code>[T=MIME-type]</code></dt>
<dd>Mime Type: declares the mime type of the target resource.</dd>
<dt><code>[]</code></dt>
<dd>specifies a character class, in which any character within the brackets will be a match. e.g., [xyz] will match either an x, y, or z.</dd>
<dt><code>[]+</code></dt>
<dd>character class in which any combination of items within the brackets will be a match. e.g., [xyz]+ will match any number of x’s, y’s, z’s, or any combination of these characters.</dd>
<dt><code>[^]</code></dt>
<dd>specifies <em>not</em> within a character class. e.g., [^xyz] will match any character that is neither x, y, nor z.</dd>
<dt><code>[a-z]</code></dt>
<dd>a dash (-) between two characters within a character class ([]) denotes the range of characters between them. e.g., [a-zA-Z] matches all lowercase and uppercase letters from a to z.</dd>
<dt><code>a{n}</code></dt>
<dd>specifies an exact number, <code>n</code>, of the preceding character. e.g., x{3} matches exactly three <code>x</code>’s.</dd>
<dt><code>a{n,}</code></dt>
<dd>specifies <code>n</code> or more of the preceding character. e.g., x{3,} matches three or more <code>x</code>’s.</dd>
<dt><code>a{n,m}</code></dt>
<dd>specifies a range of numbers, between <code>n</code> and <code>m</code>, of the preceding character. e.g., x{3,7} matches three, four, five, six, or seven <code>x</code>’s.</dd>
<dt><code>()</code></dt>
<dd>used to group characters together, thereby considering them as a single unit. e.g., (perishable)?press will match press, with or without the perishable prefix.</dd>
<dt><code>^</code></dt>
<dd>denotes the beginning of a regex (regex = regular expression) test string. i.e., begin argument with the proceeding character.</dd>
<dt><code>$</code></dt>
<dd>denotes the end of a regex (regex = regular expression) test string. i.e., end argument with the previous character.</dd>
<dt><code>?</code></dt>
<dd>declares as optional the preceding character. e.g., <code>monzas?</code> will match monza or monzas, while <code>mon(za)?</code> will match either mon or monza. i.e., <code>x?</code> matches zero or one of <code>x</code>.</dd>
<dt><code>!</code></dt>
<dd>declares negation. e.g., “<code>!string</code>” matches everything except “<code>string</code>”.</dd>
<dt><code>.</code></dt>
<dd>a dot (or period) indicates any single arbitrary character.</dd>
<dt><code>-</code></dt>
<dd>instructs “not to” rewrite the URL, as in “<code>...domain.com.* - [F]</code>”.</dd>
<dt><code>+</code></dt>
<dd>matches one or more of the preceding character. e.g., <code>G+</code> matches one or more G’s, while &#8220;+&#8221; will match one or more characters of any kind.</dd>
<dt><code>*</code></dt>
<dd>matches zero or more of the preceding character. e.g., use “<code>.*</code>” as a wildcard.</dd>
<dt><code>|</code></dt>
<dd>declares a logical “or” operator. for example, <code>(x|y)</code> matches <code>x</code> or <code>y</code>.</dd>
<dt><code>\</code></dt>
<dd>escapes special characters ( <code>^ $ ! . * |</code> ). e.g., use “<code>\.</code>” to indicate/escape a literal dot.</dd>
<dt><code>\.</code></dt>
<dd>indicates a literal dot (escaped).</dd>
<dt><code>/*</code></dt>
<dd>zero or more slashes.</dd>
<dt><code>.*</code></dt>
<dd>zero or more arbitrary characters.</dd>
<dt><code>^$</code></dt>
<dd>defines an empty string.</dd>
<dt><code>^.*$</code></dt>
<dd>the standard pattern for matching everything.</dd>
<dt><code>[^/.]</code></dt>
<dd>defines one character that is neither a slash nor a dot.</dd>
<dt><code>[^/.]+</code></dt>
<dd>defines any number of characters which contains neither slash nor dot.</dd>
<dt><code>http://</code></dt>
<dd>this is a literal statement — in this case, the literal character string, “http://”.</dd>
<dt><code>^domain.*</code></dt>
<dd>defines a string that begins with the term “<code>domain</code>”, which then may be proceeded by any number of any characters.</dd>
<dt><code>^domain\.com$</code></dt>
<dd>defines the exact string “<code>domain.com</code>”.</dd>
<dt><code>-d</code></dt>
<dd>tests if string is an existing directory</dd>
<dt><code>-f</code></dt>
<dd>tests if string is an existing file</dd>
<dt><code>-s</code></dt>
<dd>tests if file in test string has a non-zero value</dd>
</dl>
<p><strong>Redirection Header Codes</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<ul>
<li>301 &#8211; Moved Permanently</li>
<li>302 &#8211; Moved Temporarily</li>
<li>403 &#8211; Forbidden</li>
<li>404 &#8211; Not Found</li>
<li>410 &#8211; Gone</li>
</ul>
<h3 id="essentials">Essentials <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Commenting your htaccess Files</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>It is an excellent idea to consistenly and logically comment your htaccess files. Any line in an htaccess file that begins with the pound sign ( # ) tells the server to ignore it. Multiple lines require multiple pounds and use letters/numbers/dash/underscore only:</p>
<p><code># this is a comment</code><br />
<code># each line must have its own pound sign</code><br />
<code># use only alphanumeric characters along with dashes - and underscores _</code></p>
<p><strong>Enable Basic Rewriting</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Certain servers may not have “<code>mod_rewrite</code>” enabled by default. To ensure <code>mod_rewrite</code> (basic rewriting) is enabled throughout your site, add the following line once to your site’s root htaccess file:</p>
<p><code># enable basic rewriting</code><br />
<code>RewriteEngine on</code></p>
<p><strong>Enable Symbolic Links</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Enable symbolic links (symlinks) by adding the following directive to the target directory’s htaccess file. Note: for the <code>FollowSymLinks</code> directive to function, <code>AllowOverride Options</code> privileges must be enabled from within the server configuration file (<a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#ess4">see proceeding paragraph for more information</a>):</p>
<p><code># enable symbolic links</code><br />
<code>Options +FollowSymLinks</code></p>
<p><strong>Enable AllowOverride</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>For directives that require <code>AllowOverride</code> in order to function, such as <code>FollowSymLinks</code> (see above paragraph), the following directive must be added to the server configuration file. For performance considerations, it is important to only enable <code>AllowOverride</code> in the specific directory or directories in which it is required. In the following code chunk, we are enabling the <code>AllowOverride</code> privs only in the specified directory (/www/replace/this/with/actual/directory). Refer to <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#per1">this section</a> for more information about <code>AllowOverride</code> and performance enhancement:</p>
<p><code># enable allowoverride privileges</code><br />
<code>&lt;Directory /www/replace/this/with/actual/directory&gt;</code><br />
<code>AllowOverride Options</code><br />
<code>&lt;/Directory&gt;</code></p>
<p><strong>Rename the htaccess File</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Not every system enjoys the extension-only format of htaccess files. Fortunately, you can rename them to whatever you wish, granted the name is valid on your system. Note: This directive must be placed in the server-wide configuration file or it will not work:</p>
<p><code># rename htaccess files</code><br />
<code>AccessFileName ht.access</code></p>
<p>Note: If you rename your htaccess files, remember to update any associated configuration settings. For example, if you are protecting your htaccess file via <code>FilesMatch</code>, remember to inform it of the renamed files:</p>
<p><code># protect renamed htaccess files</code><br />
<code>&lt;FilesMatch "^ht\."&gt;</code><br />
<code>Order deny,allow</code><br />
<code>Deny from all</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><strong>Retain Rules Defined in httpd.conf</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Save yourself time and effort by defining replicate rules for multiple virtual hosts once and only once via your httpd.conf file. Then, simply instruct your target htaccess file(s) to inheret the httpd.conf rules by including this directive:</p>
<p><code>RewriteOptions Inherit</code></p>
<h3 id="performance">Performance <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Improving Performance via AllowOverride</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Limit the extent to which htaccess files decrease performance by enabling <code>AllowOverride</code> only in required directories. For example, if <code>AllowOverride</code> is enabled throughout the entire site, the server must dig through every directory, searching for htaccess files that may not even exist. To prevent this, we disable the <code>AllowOverride</code> in the site’s root htaccess file and then enable <code>AllowOverride</code> only in required directories via the server config file (refer to <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#ess4">this section</a> for more information). Note: if you do not have access to your site’s server config file and also need <code>AllowOverride</code> privileges, do not use this directive:</p>
<p><code># increase performance by disabling allowoverride</code><br />
<code>AllowOverride None</code></p>
<p><strong>Improving Performance by Passing the Character Set</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Prevent certain 500 error displays by passing the default character set parameter before you get there. Note: replace the “utf-8” below with the charset that your site is using:</p>
<p><code># pass the default character set</code><br />
<code>AddDefaultCharset utf-8</code></p>
<p><strong>Improving Performance by Preserving Bandwidth</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>To increase performance on PHP enabled servers, add the following directive:</p>
<p><code># preserve bandwidth for PHP enabled servers</code><br />
<code>&lt;ifmodule mod_php4.c&gt;</code><br />
<code>php_value zlib.output_compression 16386</code><br />
<code>&lt;/ifmodule&gt;</code></p>
<p><strong>Disable the Server Signature</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here we are disabling the digital signature that would otherwise identify the server:</p>
<p><code># disable the server signature</code><br />
<code>ServerSignature Off</code></p>
<p><strong>Set the Server Timezone</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here we are instructing the server to synchronize chronologically according to the time zone of some specified state:</p>
<p><code># set the server timezone</code><br />
<code>SetEnv TZ America/Washington</code></p>
<p><strong>Set the Email Address for the Server Administrator</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here we are specifying the default email address for the server administrator:</p>
<p><code># set the server administrator email</code><br />
<code>SetEnv SERVER_ADMIN default@domain.com</code></p>
<p><strong>Improve Site Transfer Speed by Enabling File Caching</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>The htaccess genius over at askapache.com explains how to dramatically improve your site’s transfer speed by enabling file caching <sup>3</sup>. Using time in seconds* to indicate the duration for which cached content should endure, we may generalize the htaccess rules as such (edit file types and time value to suit your needs):</p>
<p><code># cache images and flash content for one month</code><br />
<code>&lt;FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$"&gt;</code><br />
<code>Header set Cache-Control "max-age=2592000"</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># cache text, css, and javascript files for one week</code><br />
<code>&lt;FilesMatch ".(js|css|pdf|txt)$"&gt;</code><br />
<code>Header set Cache-Control "max-age=604800"</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># cache html and htm files for one day</code><br />
<code>&lt;FilesMatch ".(html|htm)$"&gt;</code><br />
<code>Header set Cache-Control "max-age=43200"</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># implement minimal caching during site development</code><br />
<code>&lt;FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|js|css|pdf|swf|html|htm|txt)$"&gt;</code><br />
<code>Header set Cache-Control "max-age=5"</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># explicitly disable caching for scripts and other dynamic files</code><br />
<code>&lt;FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$"&gt;</code><br />
<code>Header unset Cache-Control</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># alternate method for file caching</code><br />
<code>ExpiresActive On</code><br />
<code>ExpiresDefault A604800 # 1 week</code><br />
<code>ExpiresByType image/x-icon A2419200 # 1 month</code><br />
<code>ExpiresByType application/x-javascript A2419200 # 1 month</code><br />
<code>ExpiresByType text/css A2419200 # 1 month</code><br />
<code>ExpiresByType text/html A300 # 5 minutes</code><br />
<code># disable caching for scripts and other dynamic files</code><br />
<code>&lt;FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$"&gt;</code><br />
<code>ExpiresActive Off</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<ul class="refs">
<li>* <strong>Convert common time intervals into seconds:</strong></li>
<li>300 = 5 minutes</li>
<li>2700 = 45 minutes</li>
<li>3600 = 1 hour</li>
<li>54000 = 15 hours</li>
<li>86400 = 1 day</li>
<li>518400 = 6 days</li>
<li>604800 = 1 week</li>
<li>1814400 = 3 weeks</li>
<li>2419200 = 1 month</li>
<li>26611200 = 11 months</li>
<li>29030400 = 1 year = never expires</li>
</ul>
<p><strong>Set the default language and character set</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is an easy way to set the default language for pages served by your server (edit the language to suit your needs):</p>
<p><code># set the default language</code><br />
<code>DefaultLanguage en-US</code></p>
<p>Likewise, here we are setting the default character set (edit to taste):</p>
<p><code># set the default character set</code><br />
<code>AddDefaultCharset UTF-8</code></p>
<p><strong>Declare specific/additional MIME types</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># add various mime types</code><br />
<code>AddType application/x-shockwave-flash .swf</code><br />
<code>AddType video/x-flv .flv</code><br />
<code>AddType image/x-icon .ico</code></p>
<p><strong>Send character set and other headers without meta tags</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># send the language tag and default character set</code><br />
<code># AddType 'text/html; charset=UTF-8' html</code><br />
<code>AddDefaultCharset UTF-8</code><br />
<code>DefaultLanguage en-US</code></p>
<p><strong>Limit server request methods to GET and PUT</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># limit server request methods to GET and PUT</code><br />
<code>Options -ExecCGI -Indexes -All</code><br />
<code>RewriteEngine on</code><br />
<code>RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]</code></p>
<p><strong>Selectively process files according to server request method</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># process files according to server request method</code><br />
<code>Script PUT /cgi-bin/upload.cgi</code><br />
<code>Script GET /cgi-bin/download.cgi</code></p>
<p><strong>Execute various file types through a cgi script</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>For those special occasions where certain file types need to be processed with some specific cgi script, let em know who sent ya:</p>
<p><code># execute all png files via png-script.cgi</code><br />
<code>Action image/png /cgi-bin/png-script.cgi</code></p>
<h3 id="security">Security <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Prevent Acess to .</strong><strong>htaccess</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Add the following code block to your htaccess file to add an extra layer of security. Any attempts to access the htaccess file will result in a 403 error message. Of course, your first layer of defense to protect htaccess files involves setting htaccess file permissions via CHMOD to 644:</p>
<p><code># secure htaccess file</code><br />
<code>&lt;Files .</code><code>htaccess&gt;</code><br />
<code>order allow,deny</code><br />
<code>deny from all</code><br />
<code>&lt;/Files&gt;</code></p>
<p><strong>Prevent Acess to a Specific File</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>To restrict access to a specific file, add the following code block and edit the file name, “secretfile.jpg”, with the name of the file that you wish to protect:</p>
<p><code># prevent viewing of a specific file</code><br />
<code>&lt;files secretfile.jpg&gt;</code><br />
<code>order allow,deny</code><br />
<code>deny from all</code><br />
<code>&lt;/files&gt;</code></p>
<p><strong>Prevent acess to multiple file types</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>To restrict access to a variety of file types, add the following code block and edit the file types within parentheses to match the extensions of any files that you wish to protect:</p>
<p><code>&lt;FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$"&gt;</code><br />
<code>Order Allow,Deny</code><br />
<code>Deny from all</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><strong>Prevent Unauthorized Directory Browsing</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Prevent unauthorized directory browsing by instructing the server to serve a “xxx Forbidden &#8211; Authorization Required” message for any request to view a directory. For example, if your site is missing it’s default index page, everything within the root of your site will be accessible to all visitors. To prevent this, include the following htaccess rule:</p>
<p><code># disable directory browsing</code><br />
<code>Options All -Indexes</code></p>
<p>Conversely, to enable directory browsing, use the following directive:</p>
<p><code># enable directory browsing</code><br />
<code>Options All +Indexes</code></p>
<p>Likewise, this rule will prevent the server from listing directory contents:</p>
<p><code># prevent folder listing</code><br />
<code>IndexIgnore *</code></p>
<p id="types">And, finally, the <code>IndexIgnore</code> directive may be used to prevent the display of select file types:</p>
<p><code># prevent display of select file types</code><br />
<code>IndexIgnore *.wmv *.mp4 *.avi *.etc</code></p>
<p><strong>Change Default Index Page</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This rule tells the server to search for and serve “business.html” as the default directory index. This rule must exist in the htaccess files of the root directory for which you wish to replace the default index file (e.g., “index.html”):</p>
<p><code># serve alternate default index page</code><br />
<code>DirectoryIndex business.html</code></p>
<p>This rule is similar, only in this case, the server will scan the root directory for the listed files and serve the first match it encounters. The list is read from left to right:</p>
<p><code># serve first available alternate default index page from series</code><br />
<code>DirectoryIndex filename.html index.cgi index.pl default.htm</code></p>
<p><strong>Disguise Script Extensions</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>To enhance security, disguise scripting languages by replacing actual script extensions with dummy extensions of your choosing. For example, to change the “<code>.foo</code>” extension to “<code>.php</code>”, add the following line to your htaccess file and rename all affected files accordingly:</p>
<p><code># serve foo files as php files</code><br />
<code>AddType application/x-httpd-php .foo</code></p>
<p><code># serve foo files as cgi files</code><br />
<code>AddType application/x-httpd-cgi .foo</code></p>
<p><strong>Limit Access to the Local Area Network (LAN)</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># limit access to local area network</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order deny,allow</code><br />
<code>deny from all</code><br />
<code>allow from 192.168.0.0/33</code><br />
<code>&lt;/Limit&gt;</code></p>
<p><strong>Secure Directories by IP Address and/or Domain</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>In the following example, all IP addresses are <em>allowed access</em> except for 12.345.67.890 and domain.com:</p>
<p><code># allow all except those indicated here</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order allow,deny</code><br />
<code>allow from all</code><br />
<code>deny from 12.345.67.890</code><br />
<code>deny from .*domain\.com.*</code><br />
<code>&lt;/Limit&gt;</code></p>
<p>In the following example, all IP addresses are <em>denied access</em> except for 12.345.67.890 and domain.com:</p>
<p><code># deny all except those indicated here</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order deny,allow</code><br />
<code>deny from all</code><br />
<code>allow from 12.345.67.890</code><br />
<code>allow from .*domain\.com.*</code><br />
<code>&lt;/Limit&gt;</code></p>
<p>This is how to block unwanted visitors based on the referring domain. You can also save bandwidth by <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#types">blocking specific file types</a> — such as <code>.jpg, .zip, .mp3, .mpg</code> — from specific referring domains. Simply replace “scumbag” and “wormhole” with the offending domains of your choice:</p>
<p><code># block visitors referred from indicated domains</code><br />
<code>&lt;IfModule mod_rewrite.c&gt;</code><br />
<code>RewriteEngine on</code><br />
<code>RewriteCond %{HTTP_REFERER} scumbag\.com [NC,OR]</code><br />
<code>RewriteCond %{HTTP_REFERER} wormhole\.com [NC,OR]</code><br />
<code>RewriteRule .* - [F]</code><br />
<code>&lt;/ifModule&gt;</code></p>
<p><strong>Prevent or allow domain access for a specified range of IP addresses</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>There are several effective ways to block a range of IP addresses via htaccess. This first method blocks an IP range specified by their <acronym title="Classless Inter-Domain Routing">CIDR</acronym> (Classless Inter-Domain Routing) number. This method is useful for blocking mega-spammers such as RIPE, Optinet, and others. If, for example, you find yourself adding line after line of Apache <code>deny</code> directives for addresses beginning with the same first few numbers, choose one of them and try a <a title="whois lookup" href="http://www.whois.net/">whois lookup</a>. Listed within the whois results will be the CIDR value representing every IP address associated with that particular network. Thus, blocking via CIDR is an effective way to eloquently prevent all IP instances of the offender from accessing your site. Here is a generalized example for blocking by CIDR (edit values to suit your needs):</p>
<p><code># block IP range by CIDR number</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order allow,deny</code><br />
<code>allow from all</code><br />
<code>deny from 10.1.0.0/16</code><br />
<code>deny from 80.0.0/8</code><br />
<code>&lt;/Limit&gt;</code></p>
<p>Likewise, to allow an IP range by CIDR number:</p>
<p><code># allow IP range by CIDR number</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order deny,allow</code><br />
<code>deny from all</code><br />
<code>allow from 10.1.0.0/16</code><br />
<code>allow from 80.0.0/8</code><br />
<code>&lt;/Limit&gt;</code></p>
<p>Another effective way to block an entire range of IP addresses involves truncating digits until the desired range is represented. As an IP address is read from left to right, its value represents an increasingly specific address. For example, a fictitious IP address of 99.88.77.66 would designate some uniquely specific IP address. Now, if we remove the last two digits (66) from the address, it would represent <em>any</em> address beginning with the remaining digits. That is, 99.88.77 represents 99.88.77.1, 99.88.77.2, … 99.88.77.99, …etc. Likewise, if we then remove another pair of digits from the address, its range suddenly widens to represent every IP address 99.88.x.y, where x and y represent any valid set of IP address values (i.e., you would block 256*256 = 65,536 unique IP addresses). Following this logic, it is possible to block an entire range of IP addresses to varying degrees of specificity. Here are few generalized lines exemplifying proper htaccess syntax (edit values to suit your needs):</p>
<p><code># block IP range by address truncation</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order allow,deny</code><br />
<code>allow from all</code><br />
<code>deny from 99.88.77.66</code><br />
<code>deny from 99.88.77.*</code><br />
<code>deny from 99.88.*.*</code><br />
<code>deny from 99.*.*.*</code><br />
<code>&lt;/Limit&gt;</code></p>
<p>Likewise, to allow an IP range by address truncation:</p>
<p><code># allow IP range by address truncation</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order deny,allow</code><br />
<code>deny from all</code><br />
<code>allow from 99.88.77.66</code><br />
<code>allow from 99.88.77.*</code><br />
<code>allow from 99.88.*.*</code><br />
<code>allow from 99.*.*.*</code><br />
<code>&lt;/Limit&gt;</code></p>
<p><strong>Block or allow multiple IP addresses on one line</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Save a little space by blocking multiple IP addresses or ranges on one line. Here are few examples (edit values to suit your needs):</p>
<p><code># block two unique IP addresses</code><br />
<code>deny from 99.88.77.66 11.22.33.44</code><br />
<code># block three ranges of IP addresses</code><br />
<code>deny from 99.88 99.88.77 11.22.33</code></p>
<p>Likewise, to allow multiple IP addresses or ranges on one line:</p>
<p><code># allow two unique IP addresses</code><br />
<code>allow from 99.88.77.66 11.22.33.44</code><br />
<code># allow three ranges of IP addresses</code><br />
<code>allow from 99.88 99.88.77 11.22.33</code></p>
<p><strong>Miscellaneous rules for blocking and allowing IP addresses</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here are few miscellaneous rules for blocking various types of IP addresses. These rules may be adapted to <em>allow</em> the specified IP values by simply changing the <code>deny</code> directive to <code>allow</code>. Check ’em out (edit values to suit your needs):</p>
<p><code># block a partial domain via network/netmask values</code><br />
<code>deny from 99.1.0.0/255.255.0.0</code></p>
<p><code># block a single domain</code><br />
<code>deny from 99.88.77.66</code></p>
<p><code># block domain.com but allow sub.domain.com</code><br />
<code>order deny,allow</code><br />
<code>deny from domain.com</code><br />
<code>allow from sub.domain.com</code></p>
<p><strong>Stop Hotlinking, Serve Alternate Content</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>To serve ‘em some unexpected alternate content when hotlinking is detected, employ the following code, which will protect all files of the types included in the last line (add more types as needed). Remember to replace the dummy path names with real ones. Also, the name of the nasty image being served in this case is “eatme.jpe”, as indicated in the line containing the <code>RewriteRule</code>. Please advise that this method will also block services such as FeedBurner from accessing your images.</p>
<p><code># stop hotlinking and serve alternate content</code><br />
<code>&lt;IfModule mod_rewrite.c&gt;</code><br />
<code>RewriteEngine on</code><br />
<code>RewriteCond %{HTTP_REFERER} !^$</code><br />
<code>RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.com/.*$ [NC]</code><br />
<code>RewriteRule .*\.(gif|jpg)$ http://www.domain.com/eatme.jpe [R,NC,L]</code><br />
<code>&lt;/ifModule&gt;</code></p>
<p>Note: To deliver a standard (or custom, if configured) error page instead of some nasty image of the Fonz, replace the line containing the <code>RewriteRule</code> in the above htaccess directive with the following line:</p>
<p><code># serve a standard 403 forbidden error page</code><br />
<code>RewriteRule .*\.(gif|jpg)$ - [F,L]</code></p>
<p>Note: To grant linking permission to a site other than yours, insert this code block after the line containing the “domain.com” string. Remember to replace “goodsite.com” with the actual site domain:</p>
<p><code># allow linking from the following site</code><br />
<code>RewriteCond %{HTTP_REFERER} !^http://(www\.)?goodsite\.com/.*$ [NC]</code></p>
<p><strong>Block Evil Robots, Site Rippers, and Offline Browsers</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Eliminate some of the unwanted scum from your userspace by injecting this handy block of code. After such, any listed agents will be denied access and receive an error message instead. Please advise that there are much more comprehensive lists available this example has been truncated for business purposes. Note: DO NOT include the “[OR]” on the very last <code>RewriteCond</code> or your server will crash, delivering “500 Errors” to all page requests.</p>
<p><code># deny access to evil robots site rippers offline browsers and other nasty scum</code><br />
<code>RewriteBase /</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Anarchie [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^ASPSeek [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^attach [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^autoemailspider [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Xenu [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Zeus.*Webster [OR]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Zeus</code><br />
<code>RewriteRule ^.* - [F,L]</code></p>
<p>Or, instead of delivering a friendly error message (i.e., the last line), send these bad boys to the hellish website of your choice by replacing the <code>RewriteRule</code> in the last line with one of the following two examples:</p>
<p><code># send em to a hellish website of your choice</code><br />
<code>RewriteRule ^.*$ http://www.hellish-website.com [R,L]</code></p>
<p>Or, to send em to a virtual blackhole of fake email addresses:</p>
<p><code># send em to a virtual blackhole of fake email addresses</code><br />
<code>RewriteRule ^.*$ http://english-61925045732.spampoison.com [R,L]</code></p>
<p>You may also include specific referrers to your blacklist by using <code>HTTP_REFERER</code>. Here, we use the infamously scummy domain, “iaea.org” as our blocked example, and we use “yourdomain” as your domain (the domain to which you are blocking iaea.org):</p>
<p><code>RewriteCond %{HTTP_REFERER} ^http://www.iaea.org$</code><br />
<code>RewriteRule !^http://[^/.]\.yourdomain\.com.* - [F,L]</code></p>
<p><strong>More Stupid Blocking Tricks</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Note: Although these redirect techniques are aimed at blocking and redirecting nasty scumsites, the directives may also be employed for friendly redirection purposes:</p>
<p><code># redirect any request for anything from spamsite to differentspamsite</code><br />
<code>RewriteCond %{HTTP_REFERER} ^http://.*spamsite.*$ [NC]</code><br />
<code>RewriteRule .* http://www.differentspamsite.com [R]</code></p>
<p><code># redirect all requests from spamsite to an image of something at differentspamsite</code><br />
<code>RewriteCond %{HTTP_REFERER} ^http://.*spamsite.*$ [NC]</code><br />
<code>RewriteRule .* http://www.differentspamsite/something.jpg [R]</code></p>
<p><code># redirect traffic from a certain address or range of addresses to another site</code><br />
<code>RewriteCond %{REMOTE_ADDR} 192.168.10.*</code><br />
<code>RewriteRule .* http://www.differentspamsite.com/index.html [R]</code></p>
<p><strong>Even More Scum-Blocking Tricks</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is a step-by-step series of code blocks that should equip you with enough knowledge to block any/all necessary entities. Read through the set of code blocks, observe the patterns, and then copy, combine and customize to suit your specific scum-blocking needs:</p>
<p><code># set variables for user agents and referers and ip addresses</code><br />
<code>SetEnvIfNoCase User-Agent ".*(user-agent-you-want-to-block|php/perl).*" BlockedAgent</code><br />
<code>SetEnvIfNoCase Referer ".*(block-this-referrer|and-this-referrer|and-this-referrer).*" BlockedReferer</code><br />
<code>SetEnvIfNoCase REMOTE_ADDR ".*(666.666.66.0|22.22.22.222|999.999.99.999).*" BlockedAddress</code></p>
<p><code># set variable for any class B network coming from a given netblock</code><br />
<code>SetEnvIfNoCase REMOTE_ADDR "66.154.*" BlockedAddress</code></p>
<p><code># set variable for two class B networks 198.25.0.0 and 198.26.0.0</code><br />
<code>SetEnvIfNoCase REMOTE_ADDR "198.2(5|6)\..*" BlockedAddress</code></p>
<p><code># deny any matches from above and send a 403 denied</code><br />
<code>&lt;Limit GET POST PUT&gt;</code><br />
<code>order deny,allow</code><br />
<code>deny from env=BlockedAgent</code><br />
<code>deny from env=BlockedReferer</code><br />
<code>deny from env=BlockedAddress</code><br />
<code>allow from all</code><br />
<code>&lt;/Limit&gt;</code></p>
<p><strong>Password-Protect Directories</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is an excellent online tool for generating the necessary elements for a password-protected directory:</p>
<p><code># password protect directories</code><br />
<a title="htaccess Password Generator" rel="nofollow" href="http://www.thejackol.com/scripts/htpasswdgen.php">htaccess Password Generator</a></p>
<p><strong>Password-protect Files, Directories, and More..</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Secure site contents by requiring user authentication for specified files and/or directories. The first example shows how to password-protect any single file type that is present beneath the directory which houses the htaccess rule. The second rule employs the <code>FilesMatch</code> directive to protect any/all files which match any of the specified character strings. The third rule demonstrates how to protect an entire directory. The fourth set of rules provides password-protection for all IP’s except those specified. Remember to edit these rules according to your specific needs.</p>
<p><code># password-protect single file</code><br />
<code>&lt;Files secure.php&gt;</code><br />
<code>AuthType Basic</code><br />
<code>AuthName "Prompt"</code><br />
<code>AuthUserFile /home/path/.</code><code>htpasswd</code><br />
<code>Require valid-user</code><br />
<code>&lt;/Files&gt;</code></p>
<p><code># password-protect multiple files</code><br />
<code>&lt;FilesMatch "^(execute|index|secure|insanity|biscuit)*$"&gt;</code><br />
<code>AuthType basic</code><br />
<code>AuthName "Development"</code><br />
<code>AuthUserFile /home/path/.</code><code>htpasswd</code><br />
<code>Require valid-user</code><br />
<code>&lt;/FilesMatch&gt;</code></p>
<p><code># password-protect the directory in which this htaccess rule resides</code><br />
<code>AuthType basic</code><br />
<code>AuthName "This directory is protected"</code><br />
<code>AuthUserFile /home/path/.</code><code>htpasswd</code><br />
<code>AuthGroupFile /dev/null</code><br />
<code>Require valid-user</code></p>
<p><code># password-protect directory for every IP except the one specified</code><br />
<code># place in htaccess file of a directory to protect that entire directory</code><br />
<code>AuthType Basic</code><br />
<code>AuthName "Personal"</code><br />
<code>AuthUserFile /home/path/.</code><code>htpasswd</code><br />
<code>Require valid-user</code><br />
<code>Allow from 99.88.77.66</code><br />
<code>Satisfy Any</code></p>
<p><strong>Require SSL (Secure Sockets Layer)</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is an excellent method for requiring SSL (via askapache.com <sup>3</sup>):</p>
<p><code># require SSL</code><br />
<code>SSLOptions +StrictRequire</code><br />
<code>SSLRequireSSL</code><br />
<code>SSLRequire %{HTTP_HOST} eq "domain.tld"</code><br />
<code>ErrorDocument 403 https://domain.tld</code></p>
<p><code># require SSL without mod_ssl</code><br />
<code>RewriteCond %{HTTPS} !=on [NC]</code><br />
<code>RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]</code></p>
<p><strong>Automatically CHMOD Various File Types</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This method is great for ensuring the CHMOD settings for various file types. Employ the following rules in the root htaccess file to affect <em>all</em> specified file types, or place in a specific directory to affect only those files (edit file types according to your needs):</p>
<p><code># ensure CHMOD settings for specified file types</code><br />
<code># remember to never set CHMOD 777 unless you know what you are doing</code><br />
<code># files requiring write access should use CHMOD 766 rather than 777</code><br />
<code># keep specific file types private by setting their CHMOD to 400</code><br />
<code>chmod .</code><code>htpasswd files 640</code><br />
<code>chmod .</code><code>htaccess files 644</code><br />
<code>chmod php files 600</code></p>
<p><strong>Disguise all file extensions</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This method will disguise all file types (i.e., any file extension) and present them as .<code>php</code> files (or whichever extension you choose):</p>
<p><code># diguise all file extensions as php</code><br />
<code>ForceType application/x-httpd-php</code></p>
<p><strong>Protect against denial-of-service (DOS) attacks by limiting file upload size</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>One method to help protect your server against DOS attacks involves limiting the maximum allowable size for file uploads. Here, we are limiting file upload size to 10240000 bytes, which is equivalent to around 10 megabytes. For this rule, file sizes are expressed in bytes. Check <a title="File Size Conversion Table" rel="nofollow" href="http://www.webopedia.com/quick_ref/FileSizeConversionTable.asp">here</a> for help with various file size conversions. Note: this code is only useful if you actually allow users to upload files to your site.</p>
<p><code># protect against DOS attacks by limiting file upload size</code><br />
<code>LimitRequestBody 10240000</code></p>
<p><strong>Secure directories by disabling execution of scripts</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Prevent malicious brainiacs from actively scripting secure directories by adding the following rules to the representative htaccess file (edit file types to suit your needs):</p>
<p><code># secure directory by disabling script execution</code><br />
<code>AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi</code><br />
<code>Options -ExecCGI</code></p>
<h3 id="usability">Usability Tricks <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Minimize CSS Image Flicker in IE6</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Add the following htaccess rules to minimize or even eliminate CSS background-image “flickering” in MSIE6:</p>
<p><code># minimize image flicker in IE6</code><br />
<code>ExpiresActive On</code><br />
<code>ExpiresByType image/gif A2592000</code><br />
<code>ExpiresByType image/jpg A2592000</code><br />
<code>ExpiresByType image/png A2592000</code></p>
<p><strong>Deploy Custom Error Pages</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Replicate the following patterns to serve your own set of custom error pages. Simply replace the “<code>/errors/###.html</code>” with the correct path and file name. Also change the “###” preceding the path to summon pages for other errors. Note: your custom error pages must be larger than 512 bytes in size or they will be completely ignored by Internet Explorer:</p>
<p><code># serve custom error pages</code><br />
<code>ErrorDocument 400 /errors/400.html</code><br />
<code>ErrorDocument 401 /errors/401.html</code><br />
<code>ErrorDocument 403 /errors/403.html</code><br />
<code>ErrorDocument 404 /errors/404.html</code><br />
<code>ErrorDocument 500 /errors/500.html</code></p>
<p><strong>Provide a Universal Error Document</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># provide a universal error document</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-f</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-d</code><br />
<code>RewriteRule ^.*$ /dir/error.php [L]</code></p>
<p><strong>Employ Basic URL Spelling Check</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This bit of voodoo will auto-correct simple spelling errors in the URL:</p>
<p><code># automatically corect simple speling erors</code><br />
<code>&lt;IfModule mod_speling.c&gt;</code><br />
<code>CheckSpelling On</code><br />
<code>&lt;/IfModule&gt;</code></p>
<p><strong>Instruct browser to download multimedia files rather than display them</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is a useful method for delivering multimedia file downloads to your users. Typically, browsers will attempt to play or stream such files when direct links are clicked. With this method, provide a link to a multimedia file and a dialogue box will provide users the choice of saving the file or opening it. Here are a few htaccess rules demonstrating the technique (edit file types according to your specific needs):</p>
<p><code># instruct browser to download multimedia files</code><br />
<code>AddType application/octet-stream .avi</code><br />
<code>AddType application/octet-stream .mpg</code><br />
<code>AddType application/octet-stream .wmv</code><br />
<code>AddType application/octet-stream .mp3</code></p>
<p><strong>Instruct server to display source code for dynamic file types</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>There are many situations where site owners may wish to display the contents of a dynamic file rather than executing it as a script. To exercise this useful technique, create a directory in which to place dynamic files that should be displayed rather than executed, and add the following line of code to the htaccess file belonging to that directory. This method is known to work for <code>.pl</code>, <code>.py</code>, and <code>.cgi</code> file-types. Here it is:</p>
<p><code>RemoveHandler cgi-script .pl .py .cgi</code></p>
<p><strong>Redirect visitors to a temporary site during site development</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>During web development, maintenance, or repair, send your visitors to an alternate site while retaining full access for yourself. This is a very useful technique for preventing visitor confusion or dismay during those awkward, web-development moments. Here are the generalized htaccess rules to do it (edit values to suit your needs):</p>
<p><code># redirect all visitors to alternate site but retain full access for you</code><br />
<code>ErrorDocument 403 http://www.alternate-site.com</code><br />
<code>Order deny,allow</code><br />
<code>Deny from all</code><br />
<code>Allow from 99.88.77.66</code></p>
<p><strong>Provide a password prompt for visitors during site development</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Here is another possible solution for &#8220;hiding&#8221; your site during those private, site-under-construction moments. Here we are instructing Apache to provide visitors with a password prompt while providing open access to any specifically indicated IP addresses or URL’s. Edit the following code according to your IP address and other development requirements (thanks to Caleb at askapache.com for sharing this trick <sup>3</sup>):</p>
<p><code># password prompt for visitors</code><br />
<code>AuthType basic</code><br />
<code>AuthName "This site is currently under construction"</code><br />
<code>AuthUserFile /home/path/.</code><code>htpasswd</code><br />
<code>AuthGroupFile /dev/null</code><br />
<code>Require valid-user</code><br />
<code># allow webmaster and any others open access</code><br />
<code>Order Deny,Allow</code><br />
<code>Deny from all</code><br />
<code>Allow from 111.222.33.4</code><br />
<code>Allow from favorite.validation/services/</code><br />
<code>Allow from googlebot.com</code><br />
<code>Satisfy Any</code></p>
<p><strong>Prevent file or directory access according to specified time periods</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Prevent viewing of all pictures of Fonzi during the midnight hour — or <em>any</em> files during <em>any</em> time period — by using this handy htaccess ruleset:</p>
<p><code># prevent access during the midnight hour</code><br />
<code>RewriteCond %{TIME_HOUR} ^12$</code><br />
<code>RewriteRule ^.*$ - [F,L]</code></p>
<p><code># prevent access throughout the afternoon</code><br />
<code>RewriteCond %{TIME_HOUR} ^(12|13|14|15)$</code><br />
<code>RewriteRule ^.*$ - [F,L]</code></p>
<h3 id="redirects">Redirect Tricks <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Important Note About Redirecting via mod_rewrite</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>For all redirects using the <code>mod_rewrite</code> directive, it is necessary to have the <code>RewriteEngine</code> enabled. It is common practice to enable the <code>mod_rewrite</code> directive in either the server configuration file or at the top of the site’s root htaccess file. If the <code>mod_rewrite</code> directive is not included in either of these two places, it should be included as the first line in any code block that utilizes a rewrite function (i.e., <code>mod_rewrite</code>), but only needs to be included once for each htaccess file. The proper <code>mod_rewrite</code> directive is included here for your convenience, but may or may not also be included within some of the code blocks provided in this article:</p>
<p><code># initialize and enable rewrite engine</code><br />
<code>RewriteEngine on</code></p>
<p><strong>Redirect from http://www.domain.com to http://domain.com</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This method uses a “301 redirect” to establish a permanent redirect from the “www-version” of a domain to its respectively corresponding “non-www version”. Be sure to test <em>immediately</em> after preparing 301 redirects and remove it immediately if any errors occur. Use a “<a title="Server Header Checker" href="http://www.seoconsultants.com/tools/headers.asp">server header checker</a>” to confirm a positive 301 response. Further, always include a trailing slash “/” when linking directories. Finally, be consistent with the “www” in all links (either use it always or never).</p>
<p><code># permanently redirect from www domain to non-www domain</code><br />
<code>RewriteEngine on</code><br />
<code>Options +FollowSymLinks</code><br />
<code>RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]</code><br />
<code>RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]</code></p>
<p><strong>Redirect from http://old-domain.com to http://new-domain.com</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>For a basic domain change from “old-domain.com” to “new-domain.com” (and folder/file names have <em>not</em> been changed), use the <code>Rewrite</code> rule to remap the old domain to the new domain. When checking the redirect live, the old domain may appear in the browser’s address bar. Simply check an image path (right-click an image and select “properties”) to verify proper redirection. Remember to check your site thoroughly after implementing this redirect.</p>
<p><code># redirect from old domain to new domain</code><br />
<code>RewriteEngine On</code><br />
<code>RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]</code></p>
<p><strong>Redirect String Variations to a Specific Address</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>For example, if we wanted to redirect any requests containing the character string, “perish”, to our main page at http://perishablepress.com/, we would replace “some-string” with “perish” in the following code block:</p>
<p><code># redirect any variations of a specific character string to a specific address</code><br />
<code>RewriteRule ^some-string http://www.domain.com/index.php/blog/target [R]</code></p>
<p>Here are two other methods for accomplishing string-related mapping tasks:</p>
<p><code># map URL variations to the same directory on the same server</code><br />
<code>AliasMatch ^/director(y|ies) /www/docs/target</code></p>
<p><code># map URL variations to the same directory on a different server</code><br />
<code>RedirectMatch ^/[dD]irector(y|ies) http://domain.com</code></p>
<p><strong>Other Fantastic Redirect Tricks</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Redirect an entire site via 301:</p>
<p><code># redirect an entire site via 301</code><br />
<code>redirect 301 / http://www.domain.com/</code></p>
<p>Redirect a specific file via 301:</p>
<p><code># redirect a specific file via 301</code><br />
<code>redirect 301 /current/currentfile.html http://www.newdomain.com/new/newfile.html</code></p>
<p>Redirect an entire site via permanent redirect:</p>
<p><code># redirect an entire site via permanent redirect</code><br />
<code>Redirect permanent / http://www.domain.com/</code></p>
<p>Redirect a page or directory via permanent redirect:</p>
<p><code># redirect a page or directory</code><br />
<code>Redirect permanent old_file.html http://www.new-domain.com/new_file.html</code><br />
<code>Redirect permanent /old_directory/ http://www.new-domain.com/new_directory/</code></p>
<p>Redirect a file using RedirectMatch:</p>
<p><code># redirect a file using RedirectMatch</code><br />
<code>RedirectMatch 301 ^.*$ http://www.domain.com/index.html</code></p>
<p>Note: When redirecting specific files, use Apache‘s <code>Redirect</code> rule for files within the same domain. Use Apache‘s <code>RewriteRule</code> for <em>any</em> domains, especially if they are different. The <code>RewriteRule</code> is more powerful than the <code>Redirect</code> rule, and thus should serve you more effectively.</p>
<p>Thus, use the following for a stronger, harder page redirection (first line redirects a file, second line a directory, and third a domain):</p>
<p><code># redirect files directories and domains via RewriteRule</code><br />
<code>RewriteRule http://old-domain.com/old-file.html http://new-domain.com/new-file.html</code><br />
<code>RewriteRule http://old-domain.com/old-dir/ http://new-domain.com/new-dir/</code><br />
<code>RewriteRule http://old-domain.com/ http://new-domain.com/</code></p>
<p><strong>Send visitors to a subdomain</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>This rule will ensure that all visitors are viewing pages via the subdomain of your choice. Edit the &#8220;subdomain&#8221;, &#8220;domain&#8221;, and &#8220;tld&#8221; to match your subdomain, domain, and top-level domain respectively:</p>
<p><code># send visitors to a subdomain</code><br />
<code>RewriteCond %{HTTP_HOST} !^$</code><br />
<code>RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$ [NC]</code><br />
<code>RewriteRule ^/(.*)$ http://subdomain.domain.tld/$1 [L,R=301]</code></p>
<p><strong>More fun with RewriteCond and RewriteRule</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># rewrite only if the file is not found</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-f</code><br />
<code>RewriteRule ^(.+)special\.html?$ cgi-bin/special/special-html/$1</code></p>
<p><code># rewrite only if an image is not found</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-f</code><br />
<code>RewriteRule images/special/(.*).gif cgi-bin/special/mkgif?$1</code></p>
<p><code># seo-friendly rewrite rules for various directories</code><br />
<code>RewriteRule ^(.*)/aud/(.*)$ $1/audio-files/$2 [L,R=301]</code><br />
<code>RewriteRule ^(.*)/img/(.*)$ $1/image-files/$2 [L,R=301]</code><br />
<code>RewriteRule ^(.*)/fla/(.*)$ $1/flash-files/$2 [L,R=301]</code><br />
<code>RewriteRule ^(.*)/vid/(.*)$ $1/video-files/$2 [L,R=301]</code></p>
<p><code># broswer sniffing via htaccess environmental variables</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*</code><br />
<code>RewriteRule ^/$ /index-for-mozilla.html [L]</code><br />
<code>RewriteCond %{HTTP_USER_AGENT} ^Lynx.*</code><br />
<code>RewriteRule ^/$ /index-for-lynx.html [L]</code><br />
<code>RewriteRule ^/$ /index-for-all-others.html [L]</code></p>
<p><code># redirect query to Google search</code><br />
<code>Options +FollowSymlinks</code><br />
<code>RewriteEngine On</code><br />
<code>RewriteCond %{REQUEST_URI} .google\.php*</code><br />
<code>RewriteRule ^(.*)$ ^http://www.google.com/search?q=$1 [R,NC,L]</code></p>
<p><code># deny request according to the request method</code><br />
<code>RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD)$ [NC]</code><br />
<code>RewriteRule ^.*$ - [F]</code></p>
<p><code># redirect uploads to a better place</code><br />
<code>RewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC]</code><br />
<code>RewriteRule ^(.*)$ /cgi-bin/upload-processor.cgi?p=$1 [L,QSA]</code></p>
<p><strong>More fun with Redirect 301 and RedirectMatch 301</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># seo friendly redirect for a single file</code><br />
<code>Redirect 301 /old-dir/old-file.html http://domain.com/new-dir/new-file.html</code></p>
<p><code># seo friendly redirect for multiple files</code><br />
<code># redirects all files in dir directory with first letters xyz</code><br />
<code>RedirectMatch 301 /dir/xyz(.*) http://domain.com/$1</code></p>
<p><code># seo friendly redirect entire site to a different domain</code><br />
<code>Redirect 301 / http://different-domain.com</code></p>
<h3 id="wordpress">WordPress Tricks <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Secure WordPress Contact Forms</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Protect your insecure WordPress contact forms against online unrighteousness by verifying the domain from whence the form is called. Remember to replace the “domain.com” and “contact.php” with <em>your</em> domain and contact-form file names, respectively.</p>
<p><code># secure wordpress contact forms via referrer check</code><br />
<code>RewriteCond %{HTTP_REFERER} !^http://www.domain.com/.*$ [NC]</code><br />
<code>RewriteCond %{REQUEST_POST} .*contact.php$</code><br />
<code>RewriteRule .* - [F]</code></p>
<p><strong>WordPress Permalinks</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>In our article, <a title="In depth: htaccess rules for WordPress" href="http://perishablepress.com/press/2006/06/14/the-htaccess-rules-for-all-wordpress-permalinks/">The htaccess rules for all WordPress Permalinks</a>, we revealed the precise htaccess directives used by the WordPress blogging platform for permalink functionality. Here, for the sake of completeness, we repeat the directives only. For more details please refer to the original article:</p>
<p>If WordPress is installed in the site’s root directory, WordPress creates and uses the following htaccess directives:</p>
<p><code># BEGIN WordPress</code><br />
<code>&lt;IfModule mod_rewrite.c&gt;</code><br />
<code>RewriteEngine On</code><br />
<code>RewriteBase /</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-f</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-d</code><br />
<code>RewriteRule . /index.php [L]</code><br />
<code>&lt;/IfModule&gt;</code><br />
<code># END WordPress</code></p>
<p>If WordPress is installed in some subdirectory “foo”, WordPress creates and uses the following htaccess directives:</p>
<p><code># BEGIN WordPress</code><br />
<code>&lt;IfModule mod_rewrite.c&gt;</code><br />
<code>RewriteEngine On</code><br />
<code>RewriteBase /foo/</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-f</code><br />
<code>RewriteCond %{REQUEST_FILENAME} !-d</code><br />
<code>RewriteRule . /foo/index.php [L]</code><br />
<code>&lt;/IfModule&gt;</code><br />
<code># END WordPress</code></p>
<h3 id="random">Random Tricks <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></h3>
<p><strong>Activate SSI for HTML/SHTML file types:</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># activate SSI for HTML and or SHTML file types</code><br />
<code>AddType text/html .html</code><br />
<code>AddType text/html .shtml</code><br />
<code>AddHandler server-parsed .html</code><br />
<code>AddHandler server-parsed .shtml</code><br />
<code>AddHandler server-parsed .htm</code></p>
<p><strong>Grant CGI access in a specific directory:</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># grant CGI access in a specific directory</code><br />
<code>Options +ExecCGI</code><br />
<code>AddHandler cgi-script cgi pl</code><br />
<code># to enable all scripts in a directory use the following</code><br />
<code>SetHandler cgi-script</code></p>
<p><strong>Disable magic_quotes_gpc for PHP enabled servers:</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># turn off magic_quotes_gpc for PHP enabled servers</code><br />
<code>&lt;ifmodule mod_php4.c&gt;</code><br />
<code>php_flag magic_quotes_gpc off</code><br />
<code>&lt;/ifmodule&gt;</code></p>
<p><strong>Enable MD5 digests:</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p>Note: enabling this option may result in a relative decrease in server performance.</p>
<p><code># enable MD5 digests via ContentDigest</code><br />
<code>ContentDigest On</code></p>
<p><strong>Expression Engine Tricks:</strong> <small>[ <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#top">^</a> ]</small></p>
<p><code># send Atom and RSS requests to the site docroot to be rewritten for ExpressionEngine</code><br />
<code>RewriteRule .*atom.xml$ http://www.yoursite.com/index.php/weblog/rss_atom/ [R]</code><br />
<code>RewriteRule .*rss.xml$ http://www.yoursite.com/index.php/weblog/rss_2.0/ [R]</code></p>
<p><code># cause all requests for index.html to be rewritten for ExpressionEngine</code><br />
<code>RewriteRule /.*index.html$ http://www.domain.com/index.php [R]</code></p>
<p>Credit for creating this guied goes to <a href="http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#red7">http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#red7</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.antihostmonster.com/2009/06/htaccess-guide/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Happy New Year Hostmonster way&#8230;</title>
		<link>http://www.antihostmonster.com/2009/01/happy-new-year-hostmonster-way/</link>
		<comments>http://www.antihostmonster.com/2009/01/happy-new-year-hostmonster-way/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 07:23:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Hostmonster]]></category>
		<category><![CDATA[avoid]]></category>
		<category><![CDATA[empty]]></category>
		<category><![CDATA[error log]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[support]]></category>
		<category><![CDATA[tar]]></category>

		<guid isPermaLink="false">http://www.antihostmonster.com/?p=7</guid>
		<description><![CDATA[It seems hostmonster is cleaning their servers again.
I have been hosting with them for 18 months with few issues. I also referred more then 10 clients to them. The last referral was just yesterday and today January 10th 2009&#8230; all I noticed that my web page was taken down without a single  [...]]]></description>
			<content:encoded><![CDATA[<p>It seems hostmonster is cleaning their servers again.</p>
<p>I have been hosting with them for 18 months with few issues. I also referred more then 10 clients to them. The last referral was just yesterday and today January 10th 2009&#8230; all I noticed that my web page was taken down without a single warning&#8230;</p>
<p>Contact them and asked for a reason. They said: you are using too much server resources. I told them I had this site up and running for the last 6 months without any change in script/visitors/database so what happened?</p>
<p>The referred me to the error log to check. One of the support suggested that I should delete empty databases. I told her you allow us to use 100 db in your plan why would my 33 databases be an issue even if it is empty??</p>
<p>The reinstated my site and I started working on the error log and scripts only 5 minutes later they just took down the whole site and killed my cpanel access. I had to beg to get back my data&#8230;</p>
<p>Moving on .. AVOID hostmonster if you are thinking of a website with more than 100 visitors a day. Once you go up bit with visitors they will take your site down.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.antihostmonster.com/2009/01/happy-new-year-hostmonster-way/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

