TRICKY: Apache RewriteCond help with mobile device redirection using .htaccess

Discussion in 'Apache' started by thomas1rank, Apr 22, 2010.

  1. #1
    I am redirecting a list of know mobile devices based on their USER AGENT string. I found the code, it works great... However, I want to be able to exclude a subfolder in the following scenario:

    1. I have an .htaccess script that is parsing user agent string and redirecting to /mobile/
    (So if someone on an iPhone goes to www.domain.com it will auto-redirect them to www.domain.com/mobile)

    2. If someone from a mobile device goes to the following www.domain.com/xml/feed I want them to not be redirected (and only to that folder - all other folders are fair)

    Here is the .htaccess I have compiled so far:

    
      RewriteCond %{REQUEST_URI} !^/mobile/.*$
      RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "teli|tim-|toshi|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
      RewriteRule ^(.*)$ /mobile/ [L,R=302]
    
    Code (markup):
    I know it should be something simple like:

    RewriteCond %{REQUEST_URI} !^/xml
    Code (markup):
    But I am either getting all subfolders redirected to /mobile or an error with "too many redirects/loops/etc"

    Help please!
     
    thomas1rank, Apr 22, 2010 IP