Forum Moderators: phranque

Message Too Old, No Replies

RewiteCond block dts agent

         

halisemre

6:34 pm on Apr 18, 2010 (gmt 0)

10+ Year Member



Hi folks currently in my htaccess file I have the following rule to redirect http://example.com to http://www.example.com as follows.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


I would like to add one more RewriteCond which is below to block DTS Agent

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "dts agent" [NC]
RewriteRule .* - [F]


when I do it like this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} "dts agent" [NC]
RewriteRule .* - [F]


everyone who puts http://example.com goes to fail page. What is the safest way to do this...I would appreciate all your help

Thanks very much

[edited by: jdMorgan at 12:18 am (utc) on Apr 19, 2010]
[edit reason] example.com [/edit]

wilderness

7:24 pm on Apr 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_USER_AGENT} "dts agent" [NC]


Supplement the above line with [OR]

RewriteCond %{HTTP_USER_AGENT} "dts agent" [NC,OR]

Then add the next (ending) line
RewriteCond %{HTTP_USER_AGENT} ExName [NC]

omitting the [OR]

If you choose to add additional UA's later, than, they would also be above the closing line.
EX:
RewriteCond %{HTTP_USER_AGENT} "dts agent" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ExName1 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ExName [NC]

jdMorgan

12:30 am on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have no 'comparison' operator in your dts-agent RewriteCond.
Use either "=" for an exact-string match, or use a regular-expressions pattern, which implies a test for logical 'true'. That is, use either:

RewriteCond %{HTTP_USER_AGENT} ="dts agent" [NC]

-or-
 RewriteCond %{HTTP_USER_AGENT} ^dts\ agent$ [NC]

Be aware that both are exact-string matches, and I believe that "DTS Agent" usually appended to a standard MSIE user-agent string. In that case, this pattern will not match because the user-agent string is not exactly "dts agent" -- It only contains "dts agent". So, you may have to use a regex pattern, and leave it un-anchored:
 RewriteCond %{HTTP_USER_AGENT} dts\ agent [NC]

Consider also a request for the valid but non-canonical hostname www.example.com.:80
Your code will not redirect that request, because it only looks for "example.com" variants.

So, you might consider using something like this, if you don't require any other subdomains in addition to "www":
[code]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This rule will re-direct requests for all non-blank hostnames that are not *exactly* the canonical hostname.

Jim

halisemre

1:04 am on May 17, 2010 (gmt 0)

10+ Year Member



I am sorry for the late reply but I truly appreciate your help

jdMorgan

1:00 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put your access control rule first; There is no use wasting a domain canonicalization redirect on DTS Agent.

Also be aware that using either ="string" or ^string$ as a pattern will result in blocking only user-agents whose identifier strings are *exactly* "string". A user-agent such as "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) dts-agent" will not be blocked.

You may wish to leave the pattern un-anchored, so that all user-agents whose identifier strings *contain* "dts agent" are blocked. Further, you may wish to use the [NC] flag to make the string-comparison case-insensitive.

Jim