2

I'm trying to create a browser sniffer for Mobile Safari version, the problem is that the redirection creates an infinite loop, obviously, and I'm wondering if there's a way to do this:

http://mydomain.com (or any other requets_uri) [301]-> http://mydomain.com/ipad

Here's the snippet I'm using:

RewriteCond %{REQUEST_URI} !^ipad #Trying to set the condition "if /ipad is not the request_uri, but it doesn't work
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^(.*)$ ipad [R=301,L]

Update: Here is my (modified) full set of mod_rewrite rules:

RewriteEngine On RewriteBase /

# iOS redirection.
RewriteCond %{REQUEST_URI} !^/ipad
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^(.*)$ /ipad [R=301,L]

# If the requested URL does not exist (it's likely an agavi route),
# pass it as path info to index.php, the Agavi dispatch script.
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}      !^/favicon.ico
RewriteRule (.*) index.php?/$1 [QSA,L]

Update 2: FINAL; Thanks to the replies, I've come to the next solution which I'm posting for anybody having the same issues:

RewriteEngine On
RewriteBase /

# iOS redirection.
# Make sure the URL hasn't already been rewritten internally
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{REQUEST_URI} !^/ios
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI}      !^/favicon.ico
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^.*$ /ios [R,L]

# If the requested URL does not exist (it's likely an agavi route),
# pass it as path info to index.php, the Agavi dispatch script.
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}      !^/favicon.ico
RewriteRule (.*) index.php?/$1 [QSA,L]
lmerino
  • 173
  • 2
  • 11

1 Answers1

2

The test in your first RewriteCond will always succeed because the value of %{REQUEST_URI} always begins with a leading forward slash, and therefore can never begin with just "ipad". Try modifying it as follows:

RewriteCond %{REQUEST_URI} !^/ipad
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^(.*)$ /ipad [R=301,L]

It's also a good idea to put a forward slash before "ipad" on account of your external redirection, as I've done there. If you've set RewriteBase, this won't be an issue, but you'll probably run into trouble otherwise due to how mod_rewrite generates the new request.

Edit: The problem you're facing with your complete rule set stems from the fact that the value of the %{REQUEST_URI} will change after your second block of rules, causing the first condition to pass again when the rule set is reprocessed. This reprocessing step is part of how mod_rewrite works in a per-directory context, so you have to take it into consideration. Luckily, that's not hard to do:

RewriteEngine On
RewriteBase /

# iOS redirection.
# Make sure the URL hasn't already been rewritten internally
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteCond %{REQUEST_URI} !^/ipad
RewriteCond %{HTTP_USER_AGENT} .*Mobile.*Safari
RewriteRule ^(.*)$ /ipad [R=301,L]

# If the requested URL does not exist (it's likely an agavi route),
# pass it as path info to index.php, the Agavi dispatch script.
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}      !^/favicon.ico
RewriteRule (.*) index.php?/$1 [QSA,L]
Community
  • 1
  • 1
Tim Stone
  • 19,119
  • 6
  • 56
  • 66