Web Hosting Talk







View Full Version : looking for help with a .htaccess file


Bad Karma
07-30-2002, 05:47 PM
I'm rewritting my .htaccess file, and have gotten stuck. Anyone out there willing to look at it, and tell me what I messed up?

It traps error docs and sends the surfer to another page
It blocks hotlinking of images
It stops index browsing
It traps users from countires that I don't want to do business with

but it gives me a 500 internal server error!!@!!

here it is:

##########################################
# This block will trap all error documents and send the surfer to rnd.php

ErrorDocument 401 /rnd.php
ErrorDocument 402 /rnd.php
ErrorDocument 403 /rnd.php
ErrorDocument 404 /rnd.php
##########################################

##########################################
#This block stops surfers from being able to see empty directories

IndexIgnore *
##########################################

########################################### This block stops hotlinking by displaying a blank image

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?null.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]
##########################################

##########################################
# This block traps surfers from undesirable countries, and sends them to rnd.php

RewriteEngine On
RewriteCond %{REMOTE_HOST} ^*.at [OR]
RewriteCond %{REMOTE_HOST} ^*.bg [OR]
RewriteCond %{REMOTE_HOST} ^*.by [OR]
RewriteCond %{REMOTE_HOST} ^*.cu [OR]
RewriteCond %{REMOTE_HOST} ^*.ee [OR]
RewriteCond %{REMOTE_HOST} ^*.gh [OR]
RewriteCond %{REMOTE_HOST} ^*.hu [OR]
RewriteCond %{REMOTE_HOST} ^*.id [OR]
RewriteCond %{REMOTE_HOST} ^*.iq [OR]
RewriteCond %{REMOTE_HOST} ^*.es [OR]
RewriteCond %{REMOTE_HOST} ^*.iq [OR]
RewriteCond %{REMOTE_HOST} ^*.ir [OR]
RewriteCond %{REMOTE_HOST} ^*.lt [OR]
RewriteCond %{REMOTE_HOST} ^*.ly [OR]
RewriteCond %{REMOTE_HOST} ^*.mk [OR]
RewriteCond %{REMOTE_HOST} ^*.my [OR]
RewriteCond %{REMOTE_HOST} ^*.ro [OR]
RewriteCond %{REMOTE_HOST} ^*.ru [OR]
RewriteCond %{REMOTE_HOST} ^*.sd [OR]
RewriteCond %{REMOTE_HOST} ^*.sg [OR]
RewriteCond %{REMOTE_HOST} ^*.sk [OR]
RewriteCond %{REMOTE_HOST} ^*.th [OR]
RewriteCond %{REMOTE_HOST} ^*.ua [OR]
RewriteCond %{REMOTE_HOST} ^*.yu [OR]
RewriteCond %{REMOTE_HOST} ^*.cn
RewriteRule .*\.(htm|html)$ /rnd.php [R,NC]
##########################################

elsmore1
07-30-2002, 10:23 PM
What does your error_log say the error was?

Added later: (oops! I looked again and I can see the errors....)

All of those country extension regexps start with ^*. and they shouldn't. :)

Try something like...

^.*\.at

TMX
07-30-2002, 11:31 PM
Originally posted by elsmore1
All of those country extension regexps start with ^*. and they shouldn't. :)

Try something like...

^.*\.at

I've been working with this for about an hour now simply for the sake of learning something new (as well as trying to help the guy out).

My question is, what would be the functional difference between your example:
^.*\.at
and what I came up with:
\.at$ ?

Is there any benefit of using one over the other?

Thanks,
-Bob

priyadi
07-31-2002, 12:50 AM
Originally posted by TMX


My question is, what would be the functional difference between your example:
^.*\.at
and what I came up with:
\.at$ ?

Is there any benefit of using one over the other?


^.*\.at will match example.at, but also matches foo.at.example.com, so it might not what you want.

TMX
07-31-2002, 01:03 AM
OK, here's what I came up with. It worked great on my server (except for the no hotlinking section, which I didn't mess with), but I promise you nothing, as regular expressions and mod_rewrite were completely uncharted territory for me up until a couple of hours ago.

Troubleshooting the file was simply a matter of looking at my error and access logs and then fixing what they pointed to. Here are a couple of the sites I used as reference while sorting this out:

http://www.phpbuilder.com/columns/dario19990616.php3
http://httpd.apache.org/docs/mod/mod_rewrite.html
http://www.fantomaster.com/faarticles/modrewrite01.txt
http://www.fantomaster.com/faarticles/modrewrite02.txt
http://www.fantomaster.com/faarticles/modrewrite03.txt
http://www.fantomaster.com/faarticles/modrewrite04.txt

Anyway, try this:

# -----begin-----

# this first directive is kind of a quick-and-dirty way of turning on
# HostnameLookups, which you need in order for %{REMOTE_HOST} to
# return a host name rather than an IP. many servers do not allow
# you to turn hostnamelookups on through your .htaccess file, and
# this method is easier than modifying the domain's httpd.conf file.
# Alternately, you can turn on HostnameLookups server-wide through
# your httpd.conf file(s), but this method will give you less of
# a hit on server resources by confining lookups to this one domain
# The "order allow,deny" method may not be too graceful, but it works.

# HostNameLookups on

order allow,deny
allow from all
deny from lll.zzz


############################

# This block will trap all error documents and send the surfer to rnd.php

ErrorDocument 401 /rnd.php
ErrorDocument 402 /rnd.php
ErrorDocument 403 /rnd.php
ErrorDocument 404 /rnd.php

############################

#This block stops surfers from being able to see empty directories

Options -Indexes

############################

# This block stops hotlinking by displaying a blank image

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?null.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

############################

# This block traps surfers from undesirable countries, and sends them to rnd.php
# If you need to block by IP as well as by hostnames, use %{REMOTE_ADDR}

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REMOTE_HOST} \.at$ [OR]
RewriteCond %{REMOTE_HOST} \.bg$ [OR]
RewriteCond %{REMOTE_HOST} \.by$ [OR]
RewriteCond %{REMOTE_HOST} \.cu$ [OR]
RewriteCond %{REMOTE_HOST} \.ee$ [OR]
RewriteCond %{REMOTE_HOST} \.gh$ [OR]
RewriteCond %{REMOTE_HOST} \.hu$ [OR]
RewriteCond %{REMOTE_HOST} \.id$ [OR]
RewriteCond %{REMOTE_HOST} \.iq$ [OR]
RewriteCond %{REMOTE_HOST} \.es$ [OR]
RewriteCond %{REMOTE_HOST} \.iq$ [OR]
RewriteCond %{REMOTE_HOST} \.ir$ [OR]
RewriteCond %{REMOTE_HOST} \.lt$ [OR]
RewriteCond %{REMOTE_HOST} \.ly$ [OR]
RewriteCond %{REMOTE_HOST} \.mk$ [OR]
RewriteCond %{REMOTE_HOST} \.my$ [OR]
RewriteCond %{REMOTE_HOST} \.ro$ [OR]
RewriteCond %{REMOTE_HOST} \.ru$ [OR]
RewriteCond %{REMOTE_HOST} \.sd$ [OR]
RewriteCond %{REMOTE_HOST} \.sg$ [OR]
RewriteCond %{REMOTE_HOST} \.sk$ [OR]
RewriteCond %{REMOTE_HOST} \.th$ [OR]
RewriteCond %{REMOTE_HOST} \.ua$ [OR]
RewriteCond %{REMOTE_HOST} \.yu$ [OR]
RewriteCond %{REMOTE_HOST} \.cn$
RewriteRule .*\.(htm|html)$ /rnd.php [R,NC]

# -----end-----

Let me know how it works out.

-Bob

TMX
07-31-2002, 01:07 AM
Originally posted by priyadi


^.*\.at will match example.at, but also matches foo.at.example.com

Got it - thanks :)

-Bob

Bad Karma
07-31-2002, 12:15 PM
Thanks everyone. It's funny, I ended up on some of those pages referenced previously.

Everything works perfectly now.

If anyone wants a copy of the completed .htaccess file (works like a charge on my server), just PM me.