Forum Moderators: coopster
The scripts I've found that base redirection by host name or IP range don't seem to work. I've found two that work, but you have to list by IP, and I have several ranges to include.
Here are the four I'm looking for, and I'll post the ones I have (working) below. Also, htaccess using mod rewrite doesn't seem to work for me. It may be because I'm using mod rewrite already for search engine friendly urls. (Using WordPress and board forum software) I've found some PHP code that uses IP range or hostname, but can't get them to work.
1. PHP to redirect to another page (will say site has gone down for repairs due to major server crash) based on either hostname or IP ranges.
2. Same as 1 except one I could use in a straight html page or SSI. I start my sites off with an intro page that is index.shtml, then on to the php stuff.
3. PHP to redirect to the same repair page based on referrer. So if someone comes from xyz.com, they'll also hit the special page. (This guy has a quite a number of hate pages on me - and several others he trolls - and though he doesn't have a lot of traffic, I'd like to send his people to the same repair page to lessen the chances of him figuring it all out.
4. Same as 3 but I guess using javascript, perl or something else for reasons of #2.
The good news is that this fellah is very predictable and not very computer savvy.
Here are the codes I have that work, but they require listing every IP:
php redirect based on IP list (works perfectly):
<?php
$block = array("75.55.32.176", "11.11.188.93");if (in_array ($_SERVER['REMOTE_ADDR'], $block)) {
header("Location: http://google.com/");
exit();
}
?>
javascript version:
<script type="text/javascript">
var bannedips=["75.55.32.176", "11.11.11.11"]
var ip = '<!--#echo var="REMOTE_ADDR"-->'
var handleips=bannedips.join("¦")
handleips=new RegExp(handleips, "i")
if (ip.search(handleips)!=-1){
alert("Be back soon!")
window.location.replace("http://www.example.com/crashed.php")
}
</script>
Sorry to ask for so much, but I've been searching and searching without success.
[edited by: coopster at 7:04 pm (utc) on Dec. 28, 2006]
[edit reason] generalized domain [/edit]
I'd recommend Apache's mod_rewrite for an Apache server remote IP blocking solution, if applicable.
See the Apache WebmasterWorld forum:
[webmasterworld.com...]
# Single IP address
RewriteCond %{REMOTE_ADDR} 123.456.789.60
# Range of IPs -- 63.148.99.224-255 and 65.118.41.192-223
RewriteCond %{REMOTE_ADDR} ^63\.148\.99\.(22[4-9]¦2[3-4][0-9]¦25[0-5])$ [OR]
RewriteCond %{REMOTE_ADDR} ^63\.118\.41\.(19[2-9]¦2[0-1][0-9]¦22[0-3])$
# Redirect back to host
RewriteRule ^(.*)$ [%{REMOTE_HOST}:%{REMOTE_PORT}...] [P,R]
# Return 403 Denied error
RewriteRule .* - [F,L]
# Rewrite to outside URI
RewriteRule /* [google.com...] [L,R]
jdMorgan -- the WebmasterWorld Apache moderator -- is a supremely skilled and very generous genius.
[jdMorgan WebMasterWorld content follows]
# Block 167.0.0.1 - 167.98.98.1
#
# block 167.0.0.1 through 167.0.0.255
SetEnvIf Remote-Addr ^167\.0\.0\.([1-9]¦[1-9][0-9]¦[12][0-9][0-9])$ blockit
#
# block 167.0.1.0 through 167.0.255.255
SetEnvIf Remote-Addr ^167\.0\.([1-9][0-9]?¦[12][0-9][0-9])\. blockit
#
# block 167.1.0.0 through 167.97.255.255
SetEnvIf Remote-Addr ^167\.([1-9]¦[1-8][0-9]¦9[0-7])\. blockit
#
# block 167.98.0.0 through 167.98.97.255
SetEnvIf Remote-Addr ^167\.98\.([0-9]¦[1-8][0-9]¦9[0-7])\. blockit
#
# block from 167.98.98.0 through 167.98.98.1
SetEnvIf Remote-Addr ^167\.98\.98\.[01]$ blockit
#
Deny from env=blockit
Now, I've put in my range of IPs (I have a list of about 8 ranges). In testing, I would include a range that included my own IP. I started out by redirecting to google and all was fine.
Then I set it to redirect to a .shtml page (because I'm using a stats program that uses SSI) and I get the following message:
Too many redirects occurred trying to open “http://www.domain.com/crashed.shtml”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.
I set it back to redirect to google and all was fine. There's only one redirection: from the page you go to, to the crashed.shtml. So I'm confused.
Any ideas why this would be happening?
[jdMorgan WebmasterWorld Apache moderator code follows]
In .htaccess, rewrite looping must be explicitly prevented; Otherwise the output of the
rewrite rule will be repeatedly rewritten, with the result in this case being
/apple/apple/apple/apple/apple/.../apple/aaa.html until the server reaches its
maximum internal redirection limit, or the request becomes too long and a 414
Request-URI Too Long or a 400 Bad Request error is encountered.
One method to prevent this is to use an environment variable.
Initially, it will be undefined, and the RewriteCond will allow the rule to perform a rewrite.
But as soon as the rewrite is performed, we define the variable so that the RewriteCond will fail
on the next pass, stopping any further rewriting by this rule.
# F = 403 Forbidden
RewriteRule ^\.htaccess$ - [F]
#
RewriteCond %{ENV:RewriteDone}!^Yes$
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=RewriteDone:Yes,L]
You may change the name of the RewriteDone variable if you like. I named it to describe its function,
but the name is arbitrary; You can use any name except for the pre-defined server variable names.