On a web site of mine, I want visitors who are referred from one specific domain to be taken to a special page on the site, while everyone else continues to see the normal home page.

And the reason I know I'm not a monk yet is because it was only after three days of wrangling with Apache mod_rewrite, trying unsuccessfully to bend it to my will, that I realized I could just use perl:

#!/usr/bin/perl -w use strict; use CGI qw(referer redirect); # index.cgi my $site = 'http://mysite.com/'; my $newpage = 'newpage.html'; my $home = 'index.html'; my $q = new CGI; if ($q->referer =~ /thatdomain/){ print redirect($site . $newpage); } else { print redirect($site . $home); }

And then in my .htaccess file I say

DirectoryIndex index.cgi

and I'm done!

Three days!!!

I hope someday to get to where maybe only a day and a half goes by before I reach for perl. Will I be a monk then? :)

Replies are listed 'Best First'.
Re: How I know I'm not a monk yet
by queldor (Acolyte) on Jun 07, 2007 at 12:36 UTC
    I love perl, but I do not understand why you would want to use it for this. mod_rewrite fits the solution much better. This .htaccess should work.
    RewriteEngine on RewriteCond %{HTTP_REFERER} ^http://.*domain.com/.*$ [NC] RewriteRule ^.*$ /newpage.html [L] RewriteCond %{HTTP_REFERER} ^.*$ [NC] RewriteRule ^.*$ /index.html [L]
      Before everyone who can't hear a word against perl jumps all over queldor:

      The mod_rewrite solution is significantly better, in that it just uses an apache subrequest and returns the correct page to the initial request, where the HTML redirect requires a full extra trip between the user agent and server. (Though the cgi could do an Apache subrequest itself, at least in mod_perl 2 (not sure about mod_perl 1)).

        Yeah, mod_perl original flavour can do that too. Works well, avoids breaking the user's back button or any of that gubbins.
      The thing is, if I'd spent three fruitless days trying to get it to work in perl, I'd understand if someone said "Why would you want to use perl for this when mod_rewrite is much more suited to the job?"

      You see, I fully agree that your sample .htaccess should work. But it didn't. Three days is no exaggeration. I'm talking three full days of maybe 10 hours each where I literally did nothing but try different variations on the exact code in your example. There were probably a couple of dozen different ways that I could get it to work on my test server, but none of them worked on the live server. None!

      Then after getting nowhere for 3 days, I wrote 8 lines of perl in about a minute and it worked the first time. I like that!

      Hey, who knows, maybe given another 3 days, or maybe 6 or 60, I would've got mod_rewrite to work! But I'd rather be happy now than 60 days from now. : )

Re: How I know I'm not a monk yet
by Zaxo (Archbishop) on Jun 07, 2007 at 07:04 UTC

    The DirectoryIndex trick is not part of perl, and is a constant amazement to cgi programmers.

    Three days ensures you'll remember ;-)

    After Compline,
    Zaxo

Re: How I know I'm not a monk yet
by Mutant (Priest) on Jun 07, 2007 at 09:56 UTC
    I think one of the first rules of doing anything on Apache, *nix, Web (or probably other environments?) is if it takes more than half a day, you really should be using Perl :)
Re: How I know I'm not a monk yet
by Cody Pendant (Prior) on Jun 16, 2007 at 04:00 UTC
    I just spent, not three days but a very frustrating three hours working with RewriteRules.

    I found tons of cut-and-paste examples and so-called tutorials, but they were all pretty bad. They tell you to put Options +FollowSymLinks but they don't say why, so it could be cargo cult. They tell you to use RewriteBase / but not why, or if it's optional, and so on.

    If anyone knows a good tutorial on the subject, I'd love to hear about it.



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print