Web Hosting Talk







View Full Version : fiddle with apache


clocker1996
06-10-2002, 01:36 AM
Does anyone know how to make it so if say, the ip 216.18.0.178 visits www.yourdomain.com -- it will redirect them to another url

say www.yourdomain.com/no.html

is this even possible?

im just curious

hostNOX
06-10-2002, 01:53 AM
Yes, this is my friend, but how ? I have no clue, but i do know it can be done with .htaccess as i hosted this one customer that did it, and showed me.

roly
06-10-2002, 02:35 AM
yes if u have a dedicated server
make the default servername your ip adress and the default document root a rediect page
make www.your domain name.com an virtualhost

clocker1996
06-10-2002, 05:45 PM
i dont think you guys are talking about the same thign i am

i want it so if a certain subnet visits my website it will redirect them to another page

or a certain ip

ananthan
06-10-2002, 06:03 PM
just a option

write a small script

check this ip, if the ip is 216.18.0.178 locate:htto:/wwww.xxxx

I dont know which you are looking for

if u have questions feel free to let me know


-Ananthan

xerocity.com
06-10-2002, 06:38 PM
If you are doing what I think you are, you could copy the perl code below and save it as redirect.pl then take the .htaccess code below and add it to an .htaccess file in the main directory.

--------------- begin perl code ---------------------
#!/usr/bin/perl

$ip = "$ENV{'REMOTE_ADDR'}";
$redirip = "123.123.123.123";
$redirpage = "http://www.yourdomain.com/no.html";
$mainpage = "http://www.yourdomain.com/index.html";

if ($ip eq "$redirip") {
print "Location: $redirpage";
}
else {
print "Location: $mainpage";
}
exit;
--------------- end perl code ---------------------

--------------- begin .htaccess code ---------------------

DirectoryIndex redirect.pl index.html index.php index.html index.shtml

--------------- end .htaccess code ---------------------

The code above has not yet been tested, but it should work. If you need it to redirect a complete subnet let me know and I will make the changes.

Shyne
06-10-2002, 08:57 PM
xerocity.com,

print() will not open a new window for the the website.


#!/usr/bin/perl

$ip="$ENV{'REMOTE_ADDR'}";
$intendedip='5.5.5.5';


print "Content-type:text/html\n\n";

print "<html><head><title>Test Page</title></head>\n";
print "<body>\n";

if ($intendedip eq $ip) {
print "<script language=\"JavaScript\">";
print "window.open('http://slashdot.org')";
print "<\/script>";
} else {
print "You're not the intended IP";
}

xerocity.com
06-10-2002, 09:12 PM
Shyne,

I do not think that clocker1996 intended to have a popup appear if their ip address was the same as the one entered.

From what I am able to get from his post It looks like he wants to check to see if "XXX.XXX.XXX.XXX" visits his website and if he does send him to "this" page. If it is not "XXX.XXX.XXX.XXX" then he can go to the homepage.

Basically, it looks like he wants to either block a user or give special access to a user with a certain IP Address.

The method you provided would also be a little more sloppy in that:

1. If javascript is off your script would fail.
2. It requires javascript to generate a new window

The method I provided would be seamless; the user would never know that he/she was redirected and it does not require javascript so it would work on allmost every browser. The only browsers that this may not support is like 2.0 generation browsers (just a guestimate; but the browser probably would be at least 5 years old to not support it).

Sorry, I do not mean to bash your code. But in my opinion it is not the best code for what clocker1996 is trying to accomplish. However, only clocker1996 can judge which code would be best suited for his needs.

p.s. the print "Location: $mainpage"; will seamlessly redirect the user in the browser that is currently open to the page clocker1996 wanted them to be forewarded to.

Shyne
06-10-2002, 10:06 PM
print "Location: $mainpage"; will not redirect the user to a new website. That will simply print:

"Location http://www.yourdomain.com/index.html" in shell. (It will not print in the browser because you're missing the Content-type)

print() functions do not redirect at all.


I used window.open cause that's what I prefer but he can use anything he wants instead.

If he wants to open it in the same window he can use:

print "window.location.reload('http://slashdot.org')"

xerocity.com
06-10-2002, 11:03 PM
Correction: it is actually: print "Location: $redirpage\n\n";

I forgot the "\n\n" in my posts (I told you it was untested)

However, I know for a fact that this works, we use it all over our website and no users have ever reported any errors.

If you are using a windows based host you must use (I think this is the format)":

print "Content-type: text/html\n";
print "Location: $redirpage\n\n";

.. Yes, with only 1 "\n" on the first line; this is the only way I was able to get it to work on an nt system.

But on a *nix system you can not add the content-type because then it will print the "http://www.yourdomain.com/index.html" in the browser.

If you want to see it work (I hope this is not considered advertising; but it is supposed to be an example), click on the "home" icon below this post to visit my website. If my website loads, you have just experienced this redirection method that I explaned previously.

p.s. this script I provided is not designed for command line so it would not work correctly via that method.

Shyne
06-10-2002, 11:18 PM
The script on your website is being called through the browser differently. The variable is being fed from the Address bar directly.


#!/usr/bin/perl

$ip = "$ENV{'REMOTE_ADDR'}";
$redirip = "123.123.123.123";
$redirpage = "http://www.yourdomain.com/no.html";
$mainpage = "http://www.yourdomain.com/index.html";

if ($ip eq "$redirip") {
print "Location: $redirpage";
}
else {
print "Location: $mainpage";
}
exit;

If you were to execute this script directly (like you stated using the DirectoryIndex ) through the browser such as:

http://domain.com/script.pl

the redirection will _not_ work.

And yes I assume he's running *nix systems because of his previous posts.

xerocity.com
06-10-2002, 11:27 PM
Yes it Does work.

The example I provided is similar. If you want a true example go to http://www.xerocity.com/ it will do the redirection. We have changed our homepage through a .htaccess file to make our home page a perl script. When the script is invoked it then looks at certain information and forwards you using the method I provided to the appropriate page.

If you type in http://www.xerocity.com you will notice immediately after you press enter to go to http://www.xerocity.com your address bar will change and show http://www.xerocity.com/index.htm

This method does work.

elsmore1
06-11-2002, 01:28 AM
If I was doing it, I wouldn't use javascript (or any client-side solution) as it depends on the client configuration to work. I wouldn't use perl to do it, simply because it is so easy to get apache to do it, without the overhead of starting a cgi processor such as perl.

It should be as simple as adding something like this to an .htaccess file in the appropriate directory...

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^216\.49\.2(2[4-9]|3[0-9])\.[0-9]{1,3}
RewriteCond %{REQUEST_URI} !^.*/redirected\.html$
RewriteRule ^.* /testdir/redirected.html [R,L]


The above code should redirect anybody in the 216.49.224.0.0/20 address block to the page /testdir/redirected.html, while leaving all other requests alone. The code could easily be modified to redirect a single IP, several IPs, several IP blocks or any combination, as well as allowing those IPs to view some pages but not others, redirecting them to external hosts, etc.....

xerocity.com
06-11-2002, 01:56 AM
Originally posted by elsmore1
If I was doing it, I wouldn't use javascript (or any client-side solution) as it depends on the client configuration to work. I wouldn't use perl to do it, simply because it is so easy to get apache to do it, without the overhead of starting a cgi processor such as perl.

It should be as simple as adding something like this to an .htaccess file in the appropriate directory...

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^216\.49\.2(2[4-9]|3[0-9])\.[0-9]{1,3}
RewriteCond %{REQUEST_URI} !^.*/redirected\.html$
RewriteRule ^.* /testdir/redirected.html [R,L]


The above code should redirect anybody in the 216.49.224.0.0/20 address block to the page /testdir/redirected.html, while leaving all other requests alone. The code could easily be modified to redirect a single IP, several IPs, several IP blocks or any combination, as well as allowing those IPs to view some pages but not others, redirecting them to external hosts, etc.....

This is a good solution, we opted for the perl method over this (yes, we tried something sort of similar to this) because of the advanced logging features.

You can do it in PHP also, there are many choices that clocker1996 can choose from, he just needs to figure out what he prefers and what would work best in his situation.

clocker1996
06-11-2002, 07:55 AM
thanks all

ananthan
06-11-2002, 10:46 AM
I like the server solution ....

-ananthan

Shyne
06-11-2002, 04:17 PM
xerocity.com,

The method you sate will NOT work at all. Open a file and put this in:

#!/usr/bin/perl

print 'Location: http://www.slashdot.org';

And execute it through the browser such:

http://xerocity.com/scriptname

I GUARANTEE this will not get you anywhere.

Shyne
06-11-2002, 04:18 PM
You are using perl with an additional method which you don't mention.

xerocity.com
06-11-2002, 04:28 PM
Originally posted by Shyne
xerocity.com,

The method you sate will NOT work at all. Open a file and put this in:

#!/usr/bin/perl

print 'Location: http://www.slashdot.org';

And execute it through the browser such:

http://xerocity.com/scriptname

I GUARANTEE this will not get you anywhere.

You are right what you posted does not work.

First you need to use a quotation mark ( " ) not an apostrophe; you must tell perl to process it (perl will not proccess items in apostrophes, however it will in quotation marks ( " )

Then you need to include two (2) new line characters "\n".

A complete working example of the code you posted would be:

--------------------------------------------------------------------------
#!/usr/bin/perl

print "Location: http://www.slashdot.org\n\n";
--------------------------------------------------------------------------

I GUARANTEE you this will work!