Forum Moderators: phranque

Message Too Old, No Replies

Blocking a country with .htaccess possible

htaccess, blocking by GEO Contry code

         

jim_knopf

11:02 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



Hi all,

i got one of my newbie questions again. I have for weeks now somebody from Turkey tinkering with my WP, trying to login 50 times in 2 hours, sign up as admin etc. I see in my logs.

Can I just redirect anybody from this country somewhere with .htaccess or block them?

My htacess looks like this:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!http://www.#*$!#*$!#*$!.com
RewriteRule (wp-content/uploads.*\.(gif¦jpg¦jpeg¦png)$)¦(wp-content/gallery/zzzzzz-mix.*\.(gif¦jpg¦jpeg¦png)$) [#*$!#*$!#*$!xx.com...] [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>

I googled some around and found a older post on a forum suggesting this:

RewriteCond %{ENV:TR} ^TR$ [NC]
RewriteRule ^(.*)$ [myredirection.url...] [R,L]

So if I add this everybody from turkey get's send to my redirection.url? I'm not mind blocking complete, just dont know how.... only found the above sample with re-direct.

And to make sure - I would just add this 2 lines below the last 2 above the </ifModule>?

I have also this IP range - is there a better solution with that?

inetnum: 88.251.0.0 - 88.251.127.255

I dont care about traffic from Turkey so no mind to logout somehow...

Thanks for any advise

greetings from La Paz

jdMorgan

11:47 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you found will only work if the environment variable is set elsewhere -- Probably with a GEOip lookup performed in httpd.conf. So it's unlikely to work on your server.

I'd suggest the following code, inserted above your WP code:


RewriteCond %{REMOTE_ADDR} ^88\.251\.([1-9]?[0-9]¦1[01][0-9]¦12[0-7])$
RewriteRule .* - [F]

Alternately, you could use mod_access:

Deny from 88.251.0.0/17

Both of these snippets assume that you are *not* using a custom 403 error page. If you are, then you will have to *allow* that custom error page to be fetched by 'bad' addresses in order to prevent an error loop. You should also allow even 'bad guys' to fetch your robots.txt, or they may otherwise assume that they may spider your entire site. There are several ways to do this, depending on your needs:

mod_rewrite:


RewriteCond %{REMOTE_ADDR} ^88\.251\.([1-9]?[0-9]¦1[01][0-9]¦12[0-7])\.
RewriteRule !^(path-to-custom-403-page\.html¦robots\.txt)$ - [F]

mod_setenvif and mod_access:

SetEnvIf Request_URI "^/(path-to-custom-403-page\.html¦robots\.txt)$" allowit
Order Deny,Allow
Allow from env=allowit
Deny from 88.251.0.0/17

Replace the broken pipe "¦" characters in the patterns above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

jim_knopf

1:52 am on Jan 24, 2008 (gmt 0)

10+ Year Member



Hi Jim,

thanks for your advise. As said - absolute newbie so to verify -
the way I paste it below? ( replacing the broken pipe of course)

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!http://www.#*$!#*$!#*$!.com
RewriteCond %{REMOTE_ADDR} ^88\.251\.([1-9]?[0-9]¦1[01][0-9]¦12[0-7])$
RewriteRule .* - [F]
RewriteRule (wp-content/uploads.*\.(gif¦jpg¦jpeg¦png)$)¦(wp-content/gallery/zzzzzz-mix.*\.(gif¦jpg¦jpeg¦png)$) [#*$!#*$!#*$!xx.com...] [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>

Greetings from La Paz

jdMorgan

3:32 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, that won't work at all. With all due respect, bad code can stop your server from working immediately (if you are lucky), and can utterly ruin your search engine rankings because of some small bug that you don't notice until it is too late. Asking for someone else to check your code is no good: What if I said it was good, but I was wrong? That mistake costs me nothing, but costs you everything! For the sake of your site's health, you need to understand every single line of code that you post to your server -- what it is intended to do, how it does that, and what the side-effects are (if any).

<IfModule mod_rewrite.c>
#
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Block IP address range
RewriteCond %{REMOTE_ADDR} ^88\.251\.([1-9]?[0-9]¦1[01][0-9]¦12[0-7])$
RewriteRule .* - [F]
#
# Redirect hotlinked image requests to "nosteal.jpg"
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !http://www\.example\.com
RewriteRule ^wp-content/(uploads¦gallery/zzzzzz-mix)[^.]*\.(gif¦jpe?g¦png)$ http://www.example.com/wp-content/plugins/hotlink-protection/nosteal.jpg [R=302,L]
#
# Rewrite all URL requests that do not resolve to existing files to Wordpress script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#
</IfModule>

Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

jim_knopf

4:55 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



Hello Jim,

thank you for your time, I used the code as you placed it except replacing the broken pipes....

Yes you right regarding " what if I'm wrong" - I have to take this risk. I come here and ask because I don't know and because I don't have the monetary means to hire a professional. I will not jump on anything I see and try to educate myself on what i read here - and I have some trust.

I asume, considering the amount of posts and the advise you give that you know what you doing and trust it, considering you are human and can make mistakes also - as said I have to take the chances on that.

See, some of the code in this file been created by Wordpress when choosing the permalink option and the file is created - I have / had to trust that also and seen now on differences from your version that it's different/no correct...

Greetings from La Paz