<?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>Kashit &#187; url rewriting</title>
	<atom:link href="http://www.kashit.org/words/url-rewriting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kashit.org</link>
	<description>home to Ehsan Quddusi</description>
	<lastBuildDate>Sun, 28 Feb 2010 18:43:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>All about .htaccess files</title>
		<link>http://www.kashit.org/general-web/all-about-htaccess-files/</link>
		<comments>http://www.kashit.org/general-web/all-about-htaccess-files/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 11:01:09 +0000</pubDate>
		<dc:creator>Ehsan Quddusi</dc:creator>
				<category><![CDATA[General Web]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[error document]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[url rewriting]]></category>

		<guid isPermaLink="false">http://new.kashit.org/?p=38</guid>
		<description><![CDATA[<span class="dropcap">.h</span>taccess stands for hypertext access. It is the default name of Apache directory-level configuration file. .htaccess file provides a way to make configuration changes on a per-directory basis. .htaccess file is placed in a particular directory and the directives in the .htaccess file apply to that directory and all subdirectories thereof.The filename starts with a dot because dot files are by convention hidden files on Unix-like operating systems.]]></description>
			<content:encoded><![CDATA[<p><span class="dropcap">.h</span>taccess stands for hypertext access. It is the default name of Apache directory-level configuration file. .htaccess file provides a way to make configuration changes on a per-directory basis. .htaccess file is placed in a particular directory and the directives in the .htaccess file apply to that directory and all subdirectories thereof.The filename starts with a dot because dot files are by convention hidden files on Unix-like operating systems.</p>
<h4>A few general ideas</h4>
<p>In order to create the file, open up a text editor and save an empty page as .htaccess (or type in one character, as some editors will not let you save an empty page).</p>
<p>.htaccess files must be uploaded as ASCII mode, not BINARY. You may need to CHMOD the htaccess file to 644 or (RW-R&#8211;R&#8211;). This makes the file usable by the server, but prevents it from being read by a browser, which can seriously compromise your security. (For example, if you have password protected directories, if a browser can read the htaccess file, then they can get the location of the authentication file and then reverse engineer the list to get full access to any portion that you previously had protected. There are different ways to prevent this, one being to place all your authentication files above the root directory so that they are not www accessible, and the other is through an htaccess series of commands that prevents itself from being accessed by a browser, more on that later)</p>
<p>Most commands in htaccess are meant to be placed on one line only, so if you use a text editor that uses word-wrap, make sure it is disabled or it might throw in a few characters that annoy Apache to no end, although Apache is typically very forgiving of malformed content in an htaccess file.</p>
<p>htaccess files affect the directory they are placed in and all sub-directories, that is an htaccess file located in your root directory (yoursite.com) would affect yoursite.com/content, yoursite.com/content/contents, etc. It is important to note that this can be<br />
prevented (if, for example, you did not want certain htaccess commands to affect a specific directory) by placing a new htaccess file within the directory you don&#8217;t want affected with certain changes, and removing the specific command(s) from the new htaccess file that you do not want affecting this directory. In short, the nearest htaccess file to the current directory is treated as the htaccess file. If the nearest htaccess file is your global htaccess located in your root, then it affects every single directory in your entire site.</p>
<p>Also&#8230;some sites do not allow use of htaccess files, since depending on what they are doing, they can slow down a server overloaded with domains if they are all using htaccess files. I can&#8217;t stress this enough: You need to make sure you are allowed to use htaccess before you actually use it. Some things that htaccess can do can compromise a server configuration that has been specifically setup by the admin, so don&#8217;t get in trouble.</p>
<p>In general, you should never use .htaccess files unless you don&#8217;t have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things.</p>
<p>The use of .htaccess files effects performance. When AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.</p>
<p>.htaccess files are very popular and common among web administrators or server administrators as they provide various configuration settings for them. .htaccess files are commonly used for</p>
<ul>
<li> Authorization and Authentication to specify the security restrictions for a particular directory.</li>
<li> Redirect users from one page to another using Apache mod_rewrite.</li>
<li> Prevent directory browsing.</li>
<li> Change the default index page of a directory.</li>
<li> Block various bots.</li>
<li> Presenting custom error pages</li>
</ul>
<h4>Authorization &amp; Authentication</h4>
<p>.htaccess files are often used to specify the security restrictions for a particular directory, hence the filename &#8220;access&#8221;. The .htaccess file is often accompanied by a .htpasswd file which stores valid usernames and their passwords.</p>
<p>You must have &#8220;AllowOverride AuthConfig&#8221; in effect for these directives to be honored.</p>
<p>Contents of .htaccess file to provide authentication for a directory<br />
<code><br />
AuthType Basic<br />
AuthName "Password Required"<br />
AuthUserFile /www/passwords/password.file<br />
AuthGroupFile /www/passwords/group.file<br />
Require Group admins<br />
</code></p>
<p>Note that AllowOverride AuthConfig must be in effect for these directives to have any effect.</p>
<p>Redirect users from one page to another</p>
<p>Syntax: Redirect permanent [old directory/file name][space][new directory/file name]<br />
<code><br />
Redirect permanent /olddirectory /newdirectory<br />
Redirect permanent /olddirectory /somedirectory/newdirectory<br />
Redirect permanent /oldhtmlfile.htm /newhtmlfile.htm<br />
Redirect permanent /oldhtmlfile.htm http://your-domain.com/newhtmlfile.htm<br />
</code><br />
All the above lines are valid. Just remember to replace the file/directory names with actual ones.</p>
<h4>Using mod_rewrite rules</h4>
<p>This is an Apache module which provides rule based rewriting engine to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.</p>
<p>This module operates on the full URLs (including the path-info part) both in per-server context (httpd.conf) and per-directory context (.htaccess) and can even generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput. But all this functionality and flexibility has its drawback: complexity. So don&#8217;t expect to understand this entire module in just one day. This module was invented and originally written in April 1996 and gifted exclusively to the The Apache Group in July 1997 by &lt;a href=&#8221;http://www.engelschall.com&#8221; target=&#8221;_blank&#8221;&gt;Ralf S. Engelschall&lt;/a&gt;</p>
<h5>Examples of mod_rewrite</h5>
<p>1. Description &#8211; Your current pages are called using index.php with parameter of url i.e</p>
<p>http://www.example.com/index.php?url=category</p>
<p>and instead of this URL, you want a nice and easy to read URL like http://www.example.com/category<br />
Solution &#8211; Put the following lines in your .htaccess file.<br />
<code><br />
RewriteEngine on<br />
RewriteRule ^([^/.]+)/?$ /index.php?url=$1 [L]<br />
</code><br />
Note: If your file already contains a line ‘RewriteEngine on’ then you don’t need to put it again unless it was set to off before you putting in your lines.</p>
<p>2. Description &#8211; Your current URL is<br />
<code>http://www.example.com/index.php?cat=category&amp;subcat=subcategory</code><br />
which you would like to see as<br />
<code>http://www.example.com/category/subcategory</code><br />
Solution &#8211; Put the below lines in your .htaccess file<br />
<code><br />
RewriteEngine on<br />
RewriteRule ^([^/.]+)/([^/.]+)/?$ /index.php?cat=$1&amp;subcat=$2 [L]<br />
</code><br />
3. Description &#8211; You want to have many sub categories or categories like<br />
<code>http://www.your-domain.com/category/subcat1/subcat2/subcat3/subcat4/subcat5/</code><br />
which you would to rewrite to<br />
<code>http://www.your-domain.com/index.php?cat=category&amp;subcat1=subcat1&amp;subcat2=subcat2</code> and so on …<br />
Solution &#8211; See below lines..</p>
<p>domain.com/category –&gt; index.php?cat=category<br />
<code>RewriteRule ^([^/.]+)/?$ /index.php?cat=$1 [L]</code><br />
domain.com/category/subcategory/ –&gt; index.php?cat=category&amp;subcat=subcategory<br />
<code>RewriteRule ^([^/.]+)/([^/.]+)/?$ /index.php?cat=$1&amp;subcat=$2 [L]</code><br />
domain.com/p1/p2/p3/ –&gt; index.php?a=p1&amp;b=p2&amp;c=p3<br />
<code>RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /index.php?a=$1&amp;b=$2&amp;c=$3 [L]</code><br />
domain.com/p1/p2/p3/p4 –&gt; index.php?a=p1&amp;b=p2&amp;c=p3&amp;d=p4<br />
<code>RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ /index.php?a=$1&amp;b=$2&amp;c=$3&amp;d=$4 [L]</code><br />
4. Description &#8211; Your URL has a folder and you would like rewriting for that folder. The URL looks like this http://domain.com/folder/index.php?url=name which you want to see as http://domain.com/folder/name/<br />
Solution &#8211; Place the following lines in your .htaccess file<br />
<code><br />
RewriteEngine on<br />
RewriteRule ^folder/([^/.]+)/?$ folder/index.php?url=$1 [L]<br />
</code><br />
5. Description &#8211; Your actual URL is http://example.com/index.php?page=hello which you want to see as http://example.com/hello.htm<br />
Solution &#8211; Place the following lines in your .htaccess file<br />
<code><br />
RewriteEngine on<br />
RewriteRule ^([^/.]+).htm$ index.php?page=$1 [L]<br />
</code><br />
6. Description &#8211; Your URL is http://example.com/folder/index.php?page=hello which you want to see as http://example.com/folder/hello.htm<br />
Solution &#8211; Place the following lines in your .htaccess file<br />
<code><br />
RewriteEngine on<br />
RewriteRule ^folder/([^/.]+).htm$ folder/index.php?page=$1 [L]<br />
</code><br />
There are many more things that you can do with mod_rewrite. As and when I discover more examples, I will keep updating this page. Please feel free to post your usage of mod_rewrite if already not covered here and I will add them to the above list of examples.</p>
<h4>Prevent directory browsing</h4>
<p>When directory browsing is on, people accessing a URL from your site with no index page or no pages at all, will see a list of files and folders. To prevent such directory access, just place the following line in your .htaccess file.<br />
<code>IndexIgnore */*</code><br />
Many hosting companies, by default deny directory browsing and having said that, just in case you need to enable directory browsing, place the following line in your .htaccess file.<br />
<code>Options +Indexes</code></p>
<h4>Change the default index page of a directory</h4>
<p>Apache configuration file by default contains various file formats with index as filename as defaults for the index page. So, in case your site or directory does not has a file name which is included by default, chances are that your visitors will either see a list of all the files and folders [through directory browsing] or will not see anything at all. To change the default index page’s name for a directory or the site, place the following line in the .htaccess file of the root folder or the particular directory for which you want to change the index page’s name.<br />
<code><br />
DirectoryIndex homepage.htm<br />
DirectoryIndex somepage.htm<br />
</code><br />
To have more names, put a space between file names and it will take into considerations all those file names as possible index page names. Which means, if it finds a filename matching a list of names you supplied [in the given order] in .htaccess, then it will open that page as the index page for the directory. The below line, with multiple names, is also a valid usage:<br />
<code>DirectoryIndex homapage.html somepage.html myindexpage.html anything.html</code><br />
Remember, each entry must be in one line only.</p>
<h4>Prevent access to your .htaccess file (.htaccess security)</h4>
<p>This article would remain incomplete without mentioning this trick. To prevent visitors from viewing your .htaccess file, place the following lines in your file. Of course, by default most Apache installations will not show .htaccess file but just in case.<br />
<code><br />
&lt;Files .htaccess&gt;<br />
order allow,deny<br />
deny from all<br />
&lt;/Files&gt;<br />
</code><br />
More information and detailed documentation, visit Apache website.</p>
<h4>Presenting custom error pages</h4>
<p>Use .htaccess file to present users with your custom pages for 401 [Authorization Required], 403 [Forbidden], 404 [not found] and 500 [Internal Server Error].</p>
<p>Syntax:<br />
<code>ErrorDocument &lt; error-code &gt; &lt; location -of-custom-page&gt;</code></p>
<p>Examples:<br />
<code><br />
ErrorDocument 401 /401.html<br />
ErrorDocument 403 /403.html<br />
ErrorDocument 404 /404.html<br />
ErrorDocument 500 /500.html<br />
</code><br />
You can include some script in your customized pages to automatically send an email to you whenever those pages are called for. This way you will be notified every time a user encounters 404, 500 and other error messages.</p>
<h4>Allow/Disallow certain visitors from accessing your site</h4>
<p>To accomplish it use the following lines. Look at the syntax first:</p>
<p>Syntax:<br />
<code><br />
Order allow,deny<br />
Deny from &lt; incoming -address &gt;<br />
Allow from &lt; incoming -address&gt;<br />
</code><br />
The first line [Order allow,deny] tells what should be done first. The second line tells about denying incoming-addresses [could be a single IP, an IP block, domain name and all] and third line tells about the incoming-addresses [could be a single IP, an IP block, domain name and all] those should be allowed. If second line has ‘Deny all’, then you should change the order of allow,deny in the first line to deny,allow.</p>
<p>To deny access to a single IP address and allow everyone else<br />
<code><br />
Order allow,deny<br />
Deny from 100.100.100.1<br />
Allow from all<br />
</code><br />
To deny a block of IP address and allow everyone else. [Notice the second line]<br />
<code><br />
Order allow,deny<br />
Deny from 100.100.100.<br />
Allow from all<br />
</code><br />
To deny a single IP address and allow everyone else. [Use it to block referrals from a specific domain]<br />
<code><br />
Order allow,deny<br />
Deny from www.my-domain.com<br />
Allow from all<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kashit.org/general-web/all-about-htaccess-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
