Forum Moderators: phranque

Message Too Old, No Replies

Implementing htaccess mod rewrite help

deny from -to- RewriteCond

         

Slone

7:02 am on Feb 20, 2005 (gmt 0)

10+ Year Member



Greetings,
I am basically a newbie with htaccess, and I have decided it was time I clean up my current htaccess file to keep myself organized.

I have been using the “deny from” for a long time, which has become messy and quite a huge list. I am in need of additional control and mod rewrite structure offers me more flexibility.

I have followed several tutorials out there, but confusion has me unsure if what I am doing is correctly done.

My new htaccess looks something like this:

RewriteEngine on

# Block IP addresses
RewriteCond %{REMOTE_ADDR} ^000\.000\.000\.
RewriteCond %{REMOTE_ADDR} ^000\.000\.000\.000$

# Block Domain Referers
RewriteCond %{HTTP_REFERER} (somesite\.com) [NC,OR]
RewriteCond %{HTTP_REFERER} (somesite \.net) [NC,OR]

# Block User Agent
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^SpambotExample [OR]

RewriteRule .* - [F]

Questions:
1.) Is it okay to keep my comments “# Some comment” in my htaccess for easy reference.
2.) For each Rewrite type do I need to insert the “RewriteEngine on” before each one… or is at the top of the htaccess all that is needed?

I appreciate any feedback to help assure I don’t blow something up ;) I like the mod Rewrite structure much better.

Thanks
-S

jdMorgan

4:34 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) # comments are fine, as long as they are on a separate line
2) RewriteEngine on should only appear once at the top of your mod_rewrite [httpd.apache.org] directives, unless you've previously turned it on and then off again with RewriteEngine off.

Several problems with directive ordering, and with missing and extra [OR] flags:


Options +FollowSymlinks
RewriteEngine on
RewriteBase /
#
# Block IP addresses
RewriteCond %{REMOTE_ADDR} ^000\.000\.000\. [b][OR][/b]
RewriteCond %{REMOTE_ADDR} ^000\.000\.000\.000$ [b][OR][/b]
#
# Block Domain Referers
RewriteCond %{HTTP_REFERER} somesite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} othersite\.net [NC,OR]
RewriteCond %{HTTP_REFERER} nuthersite\.(net¦com) [NC,OR]
#
# Block User Agent
RewriteCond %{HTTP_USER_AGENT} ^SpambotExample
#
# Important -- No [OR] on last RewriteCond!
#
RewriteRule .* - [F]

Note that the broken pipe "¦" character must be replaced with a solid pipe character before you try to use this code.

Jim