custom ban page for homebrew website
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
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
).
You use an ErrorDocument
directive. People that are denied access are sent a 403 header, so:
ErrorDocument 403 banned.html
will redirect banned people to banned.html
Edit: The other alternative is to drop the mod_access stuff altogether, and use an IP-based rewrite rule (as mentioned in the question). It should just be:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} 192.168.44.201 [OR]
RewriteCond %{REMOTE_ADDR} 224.39.163.12 [OR]
RewriteCond %{REMOTE_ADDR} 172.16.7.92
RewrulteRule .* banned.html [L]
Edit 2: David beat me to an almost identical answer; I think his way is what you want
In your .htaccess file:
ErrorDocument 403 /banned.html
change /banned.html to whatever path/to/filename you want.
精彩评论