Redirecting URL with perl
Hi, First I have no idea about writing anything with perl so be gentle :-) I would like to redirect the incoming http requests randomly to a selective URL. Below is what I thought would do. #!/usr/bin/perl -w # Do I need this module ? use CGI qw(:cgi); @URL =("http://susefaq.sourceforge.net", "http://dinamizm.ath.cx", "http://toganm.tripod.com", "http://www.smaug42.com/susefaq", "http://www.bmtsolutions.com/suse_faq", "http://www.jon.fl.net.au/suse/unofficial-faq", "http://www.e-vega.info/susefaq", "http://www.ods.co.cr/susefaq", "http://www.netbulans.com/susefaq", "http://www.wdm.com/susefaq", "http://www.myxdu.com/susefaq", "http://www.tigersden.co.uk/susefaq"); srand(time |$$); $number_of_url=$#URL; $random =int(rand($numer_of_URL)); $random_URL = $URL[$random]; #We will redirect print "Content-type: text/html", "\n\n"; print "Location: $random_URL\n\n"; exit; 1) First of all is this correct 2) How do I get it work in the HTML -- Togan Muftuoglu Unofficial SuSE FAQ Maintainer http://dinamizm.ath.cx
Hi Togan, This doesn't directly answer your question but there's a perl module for apache called RandomLocation that does exactly what your trying to do. See http://www.nysaes.cornell.edu/cc/programming/perl/perldoc/Apache/RandomLocat... for examples. -- -ckm
* Christopher Mahmood;
Hi Togan, http://www.nysaes.cornell.edu/cc/programming/perl/perldoc/Apache/RandomLocat... for examples.
Thanks Chris As a matter of fact it does answer since the objective is randomly choose a mirror to got this does more than I want and is better than my perl programming, atleast I am better with Apache directives :-) -- Togan Muftuoglu Unofficial SuSE FAQ Maintainer http://dinamizm.ath.cx
On Sat, 2 Nov 2002 00:59:39 +0200
Togan Muftuoglu
I would like to redirect the incoming http requests randomly to a selective URL. Below is what I thought would do.
#!/usr/bin/perl -w # Do I need this module ? use CGI qw(:cgi);
No, you don't need this unless you want to set a cookie or receive data.
@URL =("http://susefaq.sourceforge.net", "http://dinamizm.ath.cx", "http://toganm.tripod.com", "http://www.smaug42.com/susefaq", "http://www.bmtsolutions.com/suse_faq", "http://www.jon.fl.net.au/suse/unofficial-faq", "http://www.e-vega.info/susefaq", "http://www.ods.co.cr/susefaq", "http://www.netbulans.com/susefaq", "http://www.wdm.com/susefaq", "http://www.myxdu.com/susefaq", "http://www.tigersden.co.uk/susefaq");
srand(time |$$); $number_of_url=$#URL; $random =int(rand($numer_of_URL)); ^^^^^^^^^^ $number
$random_URL = $URL[$random];
#We will redirect print "Content-type: text/html", "\n\n";
Don't print the Content-type header when you are printing a "location"; otherwise you will print the next line, instead of redirecting. If you have trouble with windows browsers, it sometimes helps to print the above line BUT ONLY with 1 "\n".
print "Location: $random_URL\n\n"; exit;
1) First of all is this correct 2) How do I get it work in the HTML
After you fix those errors, you need to fix your apache server's configuration
to use a perl script as an index.cgi.
There are a couple of ways to do it.
1. Rename the file to index.html and change the section in httpd.conf
2. Name it index.cgi and enable cgi execution in the html directory
# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
#
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.php
</IfModule>
print header(-refresh=>"1; URL=$url" ); in modperl On 2002.11.01 17:59 Togan Muftuoglu wrote:
Hi,
First I have no idea about writing anything with perl so be gentle :-)
I would like to redirect the incoming http requests randomly to a selective URL. Below is what I thought would do.
#!/usr/bin/perl -w # Do I need this module ? use CGI qw(:cgi); @URL =("http://susefaq.sourceforge.net", "http://dinamizm.ath.cx", "http://toganm.tripod.com", "http://www.smaug42.com/susefaq", "http://www.bmtsolutions.com/suse_faq", "http://www.jon.fl.net.au/suse/unofficial-faq", "http://www.e-vega.info/susefaq", "http://www.ods.co.cr/susefaq", "http://www.netbulans.com/susefaq", "http://www.wdm.com/susefaq", "http://www.myxdu.com/susefaq", "http://www.tigersden.co.uk/susefaq");
srand(time |$$); $number_of_url=$#URL; $random =int(rand($numer_of_URL));
$random_URL = $URL[$random];
#We will redirect print "Content-type: text/html", "\n\n";
print "Location: $random_URL\n\n"; exit;
1) First of all is this correct 2) How do I get it work in the HTML
--
Togan Muftuoglu Unofficial SuSE FAQ Maintainer http://dinamizm.ath.cx
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
-- __________________________ Brooklyn Linux Solutions __________________________ DRM is THEFT - We are the STAKEHOLDERS http://fairuse.nylxs.com http://www.mrbrklyn.com - Consulting http://www.inns.net <-- Happy Clients http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn.... 1-718-382-0585
participants (4)
-
Christopher Mahmood
-
Ruben I Safir
-
Togan Muftuoglu
-
zentara