Redirect iPhone, Blackberry, & Palm Requests With .htaccess
Monday, January 26th, 2009
So you wanna serve a separate page or sub domain for mobile phones? Add this bit of fun to your .htaccess and you're rockin'. All iPhone, Blackberry, & Palm requests will go to a mobile subdomain.
#redirect mobile browsers
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*BlackBerry.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Palm.*$
RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!
Article Sponsored by:
Keep up with all of our latest news, articles, and tutorials by following us on Twitter!
18 Comments
subscribe comments feedTrackbacks/Pingbacks
- AndySowards.com :: Web Development Nerdy Daily Links For 1/27/2009 | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?
- iPad Detection Using JavaScript or PHP | Shop Rate
- Is cooming » Blog Archive » iPad Detection Using JavaScript or PHP
- Redirigir iPhone, Blackberry y Palm desde .htaccess | Sentido Web
- WebDesign: iPhone erkennen und umleiten | Technokrat









Quick, easy, not big and nice; this is what I call a Mobile tutorial
. Thanks Brian.
Well said
Thanks for the comments!
If I create an iphone page and do an autoredirect to it, how can I link back to the main site? I’m using a joomla site, so all pages on the site use the index.php?pagename idea, so whenever I try to access another page on the site, I’m taken back to the iphone specific home page.
I tried implementing this code but I got an error when I tested on my iphone:
Safari cannot open page because too many redirects occurred.
How do I fix this? Thanks!
I’m getting the same issue on the iPhone… too many redirects…
I would love for this to work because of the sheer simplicity!
Any fix on this?
I am getting
Warning: Cannot modify header information – headers already sent by (output started at /home/content
and I have checked for white spaces
Nevermind, wrong forum.
hi, you forgot to add:
RewriteEngine on
in a first line. It may be required if it’s not globally on
Haha no bodies understand how to use it ….
If you have a too many redirections that means you try to redirect the user to the same domain with the same htaccess and here the loop!
The solution is too redirect the user to anohter domain with no htaccess or to redirect the user to an mobile.html page and to add a condition at the top of your htaccess.
I cannot post the complete code due to my company … but the idea is here
Thanks a lot.
@Tom,
I see the issue with the redirect loop. The page I am trying to forward to is using the same htaccess.
What is the condition syntax to “add a condition at the top of your htaccess.”
Thanks.
Here is the redirect:
#redirect mobile browsers
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://www.davidgregorywells.com/social [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://www.davidgregorywells.com/social [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*BlackBerry.*$
RewriteRule ^(.*)$ http://www.davidgregorywells.com/social [R=301]
RewriteCond %{HTTP_USER_AGENT} ^.*Palm.*$
RewriteRule ^(.*)$ http://www.davidgregorywells.com/social [R=301]
I created a subdomain(mobile.mydomain.com) that pointed to a folder (mydomain.com/mobile)
I modified my .htaccess file in my domain’s root directory with the code above.
I added a .htaccess file in the subdomain folder (mydomain.com/mobile)
with one line.
RewriteEngine off
That fixed the redirect loop for me.
this code seems to work for me for the iphone at least.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} iPhone
RewriteCond %{REQUEST_URI} !^/iphone/
RewriteRule .* /iphone/ [R]