Tech Musings

Monday, June 18, 2007

Apache and mod_rewrite

Oh, for the love of humanity. I can't believe how much time I spent today trying to decipher the esoteric and cryptic regular expression patterns of mod_rewrite. The goal was to redirect visitors coming from a specific domain to a different or separate page from what I'm going to set as the new default. Why? Because I'm preparing to change over the default home page in my domain (mydomain.com) to a FLASH version. BUT, if visitors come from one specific domain (www.thisotherdomain.net) I want them to be redirected to the old PHP page, not the new Flash version.

Using Apache's mod rewrite module I was able to do this but only because I finally found the following helpful forum thread:

http://www.webmasterworld.com/apache/3361785.htm

Here's the example of how to write the .htaccess code for it:

Options +FollowSymlinks
RewriteEngine on
RewriteCond $1 !^old_index.php
RewriteCond %{HTTP_REFERER}^https?://([^.]+\.)*thisotherdomain\.net
RewriteRule ^(.*) http://mydomain.com/old_index.php [R=302,L]

The first rewrite condition is needed because Firefox originally complained about an infinite loop. Interesting, it was much easier to find examples on the Web for this little Apache magic when setting up a redirect based on NOT coming from a specific domain. The example above is nice because it will redirect even if the referring link is from a domain or subdomain on http://www.thisotherdomain.net. Also, it is important to remove the "$" pattern end anchor that I normally place at the end of RewriteCond statements in my .htaccess files.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home