Forum Moderators: phranque

Message Too Old, No Replies

Banning top level domain

using .htaccess

         

obono

4:17 pm on Apr 23, 2005 (gmt 0)

10+ Year Member



Does somebody know if this might work for banning a domain extension?

RewriteCond %{HTTP_REFERER} ^http://[a-z.-]+.ru/.*$ [NC,OR]
RewriteRule ^(.*)$ %1 [R=301,L]

jd01

7:08 am on Apr 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi obono,

I think you are close, but his may be more effective:

RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^!(www.)?yoursite\.com$
RewriteCond %{HTTP_HOST} ^.+\.ru [OR]
RewriteCond %{HTTP_REFERER} ^.+\.ru
RewriteRule . - [F]

This should work if your site is being framed, or if there are referers coming from sites with the .ru extension...

The first condition checks to see if there is a referer. If there is not, the ruleset will fail.

The second condition checks to see if you are the referer. If your site is, the ruleset will fail. (www.)? makes the www portion of your URL optional.
The idea is to keep your performance, so if the rule set fails as soon as you are the referer, only external referers will be checked by the remaining conditions.

The third and fourth condition check to see if either the host (framing) or the referer are from anything .ru, and if they are, returns forbiden.

By not formally closing the line with $ 'and anything else' is assumed.

You might need to 'tweek' it a little, but as it is it's fairly close.

Please, keep in mind this will not block any search engines or browsers without referers.

Hope this helps.

Justin

obono

7:44 am on Apr 25, 2005 (gmt 0)

10+ Year Member



Thank you!

sitz

1:34 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^!(www.)?yoursite\.com$
RewriteCond %{HTTP_HOST} ^.+\.ru [OR]
RewriteCond %{HTTP_REFERER} ^.+\.ru
RewriteRule . - [F]

The second RewriteCond should, I think, be written as:


RewriteCond %{HTTP_REFERER} ^(https?://)?!(www\.)?yoursite\.com

Since a) you want that first '.' to be take literally, and b) HTTP_REFERER gets the full URL of the referring page, not just the fqdn. Additionally, third Cond can potentially break things, and the fourth Cond has a higher probablity of doing so, since it will match on *ANY* url contained '.ru', including things like 'www.run-lola-run.com' or even 'www.example.com/info/running.in.place.html'

However, unless the site you're running is a .ru domain, this can be vastly simplified:


RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^(https?://)?.+\.ru($¦/)
RewriteRule . - [F,L]

Additionally, mod_rewrite is actually overkill for this, since the same thing can be accomplished using SetEnvIfNoCase:


SetEnvIfNoCase Referer ^(https?://)?.+\.ru($¦/) ban

...and then in your allow/deny rules:

Order allow,deny
Allow from all
Deny from env=ban