views:

49

answers:

3

I understand how to ban an IPs address from my apache webserver using .htaccess:

order allow,deny
deny from 192.168.44.201
deny from 224.39.163.12
deny from 172.16.7.92
allow from all

I'd like to create a custom "You've been banned" page. How could I do this?

EDIT:

To clarify, I am not trying to create a custom 403 page, as these are used in other instances as well (i.e. failed basic authentication, etc). The closest I have come so far is:

rewritecond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteRule !^banned$ /banned [NC,L]

but this produces an internal server error when the IP is matched, instead of sending the user to /banned

+2  A: 
Michael Mrozek
While this is indeed useful, it would result in "banned" messages every time someone hits a 403 error
Mala
@Mala Well...yes, isn't that the point? Do you serve 403 error codes for situations besides this one?
Michael Mrozek
if file permissions are set for a user not to be able to read a document, or fails basic authentication, he should get a 403 error. However, he won't be banned
Mala
+1  A: 

In your .htaccess file:

ErrorDocument 403 /banned.html 

change /banned.html to whatever path/to/filename you want.

Crayon Violent
+3  A: 

The other answers which suggest an ErrorDocument for the 403 code would be the usual way to do this. But since you want to show a different error page if the user is denied access based on IP (as opposed to other reasons), you can use mod_rewrite, as you suspected.

RewriteCond %{REMOTE_ADDR} =192.168.44.201 [OR]
RewriteCond %{REMOTE_ADDR} =224.39.163.12 [OR]
RewriteCond %{REMOTE_ADDR} =172.16.7.92
RewriteRule !^/banned.html /banned.html [L]

P.S. This should go in your virtual host configuration, not an .htaccess file, if at all possible. If you don't have access to the virtual host configuration file, you could put it in a .htaccess file, but remove the leading slash from the RewriteRule pattern (so !^/banned.html becomes !^banned.html).

David Zaslavsky
Thank you, it was that leading slash that was messing with my internal server errors. I'm running into another problem though - I already have a rewrite rule in my htaccess file, and I'm getting internal server errors if i don't comment out the first one. What am I doing wrong?
Mala
That's hard to tell without seeing what the first rewrite rule is.
David Zaslavsky
ah sorry. Here it is:http://flyingmonkey.nfshost.com/misc_files/htaccess.txt
Mala
Nevermind, I got it! stupid glaring error on my part
Mala