1

I would like to prevent iPads from connecting to my server, and only allow iPhones. What is wrong with this mod_rewrite script that is allowing this?

#Prevent non-iPhones from connecting to the server.
#RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone2C1.* [NC]
#RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone3C1.* [NC]
#RewriteRule () http://www.xyz.com/ [R,NC,L]



#The next line prevents version 4.0
RewriteCond %{HTTP_USER_AGENT} .*801.293.* [NC]

#The next line prevents version 3.13
RewriteCond %{HTTP_USER_AGENT} .*705.18.* [NC]

#The next line prevents version 3.21
RewriteCond %{HTTP_USER_AGENT} .*702.405.* [NC]

#The next line prevents version 3.2
RewriteCond %{HTTP_USER_AGENT} .*702.367.* [NC]

#Require iPhones to be 3GS or iPhone 4.
RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone2C1.* [NC]
RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone3C1.* [NC]

The offending device has this for a user agent:

 Apple-iPad1C1/803.148 

2 Answers 2

2

The RewriteCond conditions are applied by default using a logical AND, and you need to use OR since you are specifying what versions to block instead of what versions to allow:

#The next line prevents version 4.0
RewriteCond %{HTTP_USER_AGENT} .*801.293.* [NC,OR]

#The next line prevents version 3.13
RewriteCond %{HTTP_USER_AGENT} .*705.18.* [NC,OR]

#The next line prevents version 3.21
RewriteCond %{HTTP_USER_AGENT} .*702.405.* [NC,OR]

#The next line prevents version 3.2
RewriteCond %{HTTP_USER_AGENT} .*702.367.* [NC,OR]

#Require iPhones to be 3GS or iPhone 4.
RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone2C1.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !.*Apple-iPhone3C1.* [NC,OR]

RewriteRule .* http://destination.example.com/ [R,NC,L]
0
0

I think you are missing a RewriteRule

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Browser Dependent Content Description:

At least for important top-level pages it is sometimes necessary to provide the optimum of browser dependent content, i.e. one has to provide a maximum version for the latest Netscape variants, a minimum version for the Lynx browsers and a average feature version for all others.

Solution: We cannot use content negotiation because the browsers do not provide their type in that form. Instead we have to act on the HTTP header "User-Agent". The following condig does the following: If the HTTP header "User-Agent" begins with "Mozilla/3", the page foo.html is rewritten to foo.NS.html and and the rewriting stops. If the browser is "Lynx" or "Mozilla" of version 1 or 2 the URL becomes foo.20.html. All other browsers receive page foo.32.html. This is done by the following ruleset:

RewriteCond %{HTTP_USER_AGENT}  ^Mozilla/3.*
RewriteRule ^foo\.html$         foo.NS.html          [L]

RewriteCond %{HTTP_USER_AGENT}  ^Lynx/.*         [OR]
RewriteCond %{HTTP_USER_AGENT}  ^Mozilla/[12].*
RewriteRule ^foo\.html$         foo.20.html          [L]

RewriteRule ^foo\.html$         foo.32.html          [L]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .