<?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>Tony Bhimani's Blog &#187; Apache</title>
	<atom:link href="http://www.tonybhimani.com/category/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tonybhimani.com</link>
	<description>Where I Share my Linux and Programming Experiences</description>
	<lastBuildDate>Sun, 01 Jun 2008 02:48:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Domain Redirection using Apache mod_rewrite and .htaccess</title>
		<link>http://www.tonybhimani.com/2008/01/26/domain-redirection-using-apache-mod_rewrite-and-htaccess/</link>
		<comments>http://www.tonybhimani.com/2008/01/26/domain-redirection-using-apache-mod_rewrite-and-htaccess/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 04:21:49 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[BIND]]></category>
		<category><![CDATA[HOWTOs]]></category>
		<category><![CDATA[domain redirection]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[URL rewrite]]></category>

		<guid isPermaLink="false">http://www.tonybhimani.com/2008/01/26/domain-redirection-using-apache-mod_rewrite-and-htaccess/</guid>
		<description><![CDATA[I recently acquired more XenoCafe domains (xenocafe.info, xenocafe.net, and xenocafe.org) and finally got around to adding them to my server. Instead of them being their own sites (well, point to my primary site,  but the URL in the browser address bar is taken over by the new domain name), I wanted them to redirect [...]]]></description>
			<content:encoded><![CDATA[<p>I recently acquired more XenoCafe domains (<a href="http://xenocafe.info/" title="XenoCafe Linux Tutorials for Beginners" target="_blank">xenocafe.info</a>, <a href="http://xenocafe.net/" title="XenoCafe Linux Tutorials for Beginners" target="_blank">xenocafe.net</a>, and <a href="http://xenocafe.org/" title="XenoCafe Linux Tutorials for Beginners" target="_blank">xenocafe.org</a>) and finally got around to adding them to my server. Instead of them being their own sites (well, point to my primary site,  but the URL in the browser address bar is taken over by the new domain name), I wanted them to redirect to my primary domain (<a href="http://xenocafe.com/" title="XenoCafe Linux Tutorials for Beginners" target="_blank">xenocafe.com</a>).</p>
<p>If you want to achieve the same effect, here is how I did it (to see it in action, click on the first three XenoCafe links above). All you need is Apache, mod_rewrite, and htaccess. I&#8217;m going to skip the step-by-step configuration stuff to cut down the length of this article. If you need help, you can register for an account and post a question to the comments section for this post.</p>
<p>In this mock setup, I&#8217;ll use 10.10.100.34 as the IP address of my web server, example.com as the primary domain I want to redirect to, and ourexamples.com as the domain I want to redirect from (to example.com). This is a three step process that involves creating the zone file(s), adding the redirect domains to your Apache httpd.conf VirtualHost configuration, and finally creating the htaccess mod_rewrite rules to redirect all requests for the new domains to your desired domain.</p>
<p><strong>Create a New Zone File for each Domain you Acquired</strong></p>
<p>The primary domain example.com already has a zone file and is a functioning web site, but for ourexamples.com to work we need to create a zone file for it. Here is an example (we only need the minimum &#8211; SOA, NS, and the host for web).</p>
<pre class="code">$TTL   21600
$ORIGIN ourexamples.com.
@       IN      SOA     ns1.example.com. hostmaster.example.com. (
                        2008012601      ; serial
                        3600            ; refresh
                        600             ; retry
                        86400           ; expiry
                        21600 )         ; minimum

        IN      NS      ns1.example.com.
        IN      NS      ns2.example.com.

        IN      A       10.10.100.34
www     IN      A       10.10.100.34</pre>
<p>Now reload the zone(s) for changes to take effect.</p>
<p><strong>Add the Redirect Domains to the Primary Domain&#8217;s VirtualHost Definition (using the ServerAlias directive)</strong></p>
<p>Edit your primary domain&#8217;s virtual host entry and add the new domain(s) using the <a href="http://httpd.apache.org/docs/2.0/mod/core.html#serveralias" title="Apache ServerAlias Directive" target="_blank">ServerAlias</a> directive. Below is an example of editing the example.com virtual host and adding the new redirection domain ourexamples.com (highlighted line).</p>
<pre class="code">&lt;VirtualHost *:80&gt;
    ServerAdmin hostmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    <span style="background-color: #ffff00">ServerAlias ourexamples.com www.ourexamples.com</span>
    DocumentRoot /web/example/html
    ScriptAlias /cgi-bin/ /web/example/html/cgi-bin/
    ErrorLog /web/example/logs/error_log
    CustomLog /web/example/logs/access_log combined
    &lt;Directory "/web/example/html"&gt;
        AllowOverride All
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;</pre>
<p>Restart Apache to reload the virtual host changes. Next we&#8217;ll need to create the htaccess file to configure domain redirection.</p>
<p><strong>Using mod_rewrite to Redirect the Domain Requests</strong></p>
<p>If we were to stop here, the new domain would work, however there is no redirection to your primary domain name. It will simply use the same document root and serve the files without any changes to the URL in the browser address bar. If this is what you desire, then stop here, otherwise continue on to bounce all requests from ourexamples.com to example.com.</p>
<p>In the document root of where your HTML files are stored (where DocumentRoot points to in your Apache VirtualHost definition), create a .htaccess with a text editor. Here is the example.</p>
<pre class="code">&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
&lt;/IfModule&gt;</pre>
<p>In short, what this code does is turn on mod_rewrite&#8217;s engine, check the HTTP host to see if it matches www.example.com and also checks for empty references; if either case is a yes it rewrites the URL to http://www.example.com using a permanent redirect (HTTP 301 Redirection header). The $1 appends any portion trailing the domain name from the original (directories and/or pages being accessed). This code also forces the &#8216;www&#8217; prefix on all requests &#8212; <em>http://example.com </em>=&gt;<em> http://www.example.com</em> and <em>http://ourexamples.com </em>=&gt;<em> http://www.example.com</em> and so on</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tonybhimani.com/2008/01/26/domain-redirection-using-apache-mod_rewrite-and-htaccess/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hotlink Protection with Apache mod_rewrite and .htaccess</title>
		<link>http://www.tonybhimani.com/2007/11/02/hotlink-protection-with-apache-mod_rewrite-and-htaccess/</link>
		<comments>http://www.tonybhimani.com/2007/11/02/hotlink-protection-with-apache-mod_rewrite-and-htaccess/#comments</comments>
		<pubDate>Fri, 02 Nov 2007 17:39:46 +0000</pubDate>
		<dc:creator>Tony</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[HOWTOs]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.tonybhimani.com/2007/11/02/hotlink-protection-with-apache-mod_rewrite-and-htaccess/</guid>
		<description><![CDATA[So you have people linking to files on your site and it&#8217;s eating up all your bandwidth. You want it to stop but you&#8217;re not sure how. If you&#8217;re using Apache for your web server, you can use .htaccess to block the file requests from remote servers and put an end to your bandwidth theft.
I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>So you have people linking to files on your site and it&#8217;s eating up all your bandwidth. You want it to stop but you&#8217;re not sure how. If you&#8217;re using Apache for your web server, you can use .htaccess to block the file requests from remote servers and put an end to your bandwidth theft.</p>
<p>I&#8217;m writing this article because I&#8217;m a victim of people placing links to my images on their site&#8217;s without my permission, slowly eating away at the bandwidth I&#8217;m paying for, not them. If you&#8217;re reading this, it&#8217;s likely that the same thing is happening to you and you&#8217;re sick of it. With that said, let&#8217;s get started.</p>
<p>I make the assumption that you&#8217;re using Apache and have root access to your server because you&#8217;ll need to see if you have mod_rewrite enabled. If you happen to be in a hosted environment, log in to your control panel and check the documentation for managing .htaccess files. Some control panels like CPanel already have built-in modules that are designed to handle hotlink protection. If you have a dedicated server, log in through SSH or locally if you have that ability.</p>
<p>1. Check to see if the mod_rewrite module is enabled in the Apache httpd.conf file.</p>
<pre class="code">nano /etc/httpd/conf/httpd.conf</pre>
<p>Look for the &#8220;Dynamic Shared Object (DSO) Support&#8221; section and make sure you have the following line.</p>
<pre class="code">LoadModule rewrite_module modules/mod_rewrite.so</pre>
<p>If it is commented out with a # before LoadModule, delete the # and save the change. Restart Apache with</p>
<pre class="code">service httpd restart</pre>
<p>or</p>
<pre class="code">/etc/init.d/httpd restart</pre>
<p>for the change to take effect. If mod_rewrite is completely missing from your Apache config, then you may want to take a look at this article on how to <a href="http://linuxadministration.wordpress.com/2007/08/17/configure-apache/" target="_blank">configure Apache</a>.</p>
<p>2. Create a .htaccess in your web site&#8217;s html root directory or a subdirectory where you want to disable hotlinking. For this example, I only want to disable hotlinking to all image types (gif, jpeg, png, and bmp bitmaps) and have Apache return a HTTP 403 Forbidden error to the leech site. You can block any file type you want, such as pdf&#8217;s, mp3&#8217;s, etc. You could also serve up an error image to the leech saying something to the effect of &#8220;This image was hotlinked from www.yourdomain.com.&#8221;</p>
<pre class="code">touch .htaccess
nano .htaccess</pre>
<p>3. Add this code to your .htaccess file, make the appropriate changes like changing yourdomain.com to your actual domain name, and finally save the .htaccess file.</p>
<pre class="code">&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [NC,F]
&lt;/IfModule&gt;</pre>
<p>The matching is done with regular expressions, so you&#8217;ll need to know that before you can do complex cases. In short, what this code does is first look for empty referrers, matches your domain by any or no specified subdomains (case insensitive [NC]), rewrite the URL if any jpeg, jpg, gif, bmp, or png is requested (case insensitive [NC]) with a 403 Forbidden ([F]).</p>
<p>By issuing a 403 Forbidden, no image content will be sent back to the leech server, therefore your bandwidth will not be bled dry by hotlinking. As I mentioned earlier, you can send back an error image in place of the actual requested image, however you&#8217;ll need to change the RewriteRule line to something like this.</p>
<pre class="code">RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/sorry.jpe [L]</pre>
<p>Take note at the returned image file name. We send back a jpeg variant (JPE) because we&#8217;re set on blocking the other image types. If you send back a file type that you&#8217;re blocking, you&#8217;ll likely get a 500 Internal Server Error like I did when I first set up hotlink protection. It makes sense, why send back what you&#8217;re blocking in the first place. I opted for the 403 Forbidden approach for my sites instead.</p>
<p><strong>Sources:</strong><br />
<a href="http://altlab.com/htaccess_tutorial.html" target="_blank">http://altlab.com/htaccess_tutorial.html</a><br />
<a href="http://www.dagondesign.com/articles/hotlink-protection-with-htaccess/">http://www.dagondesign.com/articles/hotlink-protection-with-htaccess/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tonybhimani.com/2007/11/02/hotlink-protection-with-apache-mod_rewrite-and-htaccess/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
