iPhone formatted webpage: Redirecting to it

DisneyEcho

New Member
Bronze
Jul 14, 2007
54
0
0
disneyecho.emuck.com
#1
Does anyone know how to detect if a person is using an iPhone when viewing a webpage and then automatically redirect their browser to a different page?

For example, I have a page listing hot topics on my forum, found here:
http://disneyecho.emuck.com/topics/index.html

I also have an iphone-specific version here:
http://disneyecho.emuck.com/iPhone/index.html

What I'd like to do is automatically redirect people using iPhones who open the http://disneyecho.emuck.com/topics/index.html webpage over to the http://disneyecho.emuck.com/iPhone/index.html webpage.

Can someone let me know how to do this? Thanks in advance!
 

JoeT

Member
Silver
Dec 18, 2007
911
0
16
Tampa, FL
#2
Does anyone know how to detect if a person is using an iPhone when viewing a webpage and then automatically redirect their browser to a different page?
What I'd like to do is automatically redirect people using iPhones who open the http://disneyecho.emuck.com/topics/index.html webpage over to the http://disneyecho.emuck.com/iPhone/index.html webpage.

Can someone let me know how to do this? Thanks in advance!


Since you're using Apache, this is easy with a mod_rewrite directive in your httpd.conf:


RewriteCond %{HTTP_USER_AGENT} .*iPhone.*
RewriteRule ^topics/index\.html$ iPhone/index.html [L]


Note: I haven't tried this yet on my own web server (thinking about rewriting one simple app for the iPhone but haven't had the time yet), but the HTTP_User_Agent definitely should match. Here's an excerpt from my log from an iPhone access:

[30/Dec/2007:11:52:41 -0500] "GET /fcc HTTP/1.1" 301 232 "-" "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3"
 

meiphones_brill

New Member
Silver
Nov 16, 2007
797
0
0
#3
Since you're using Apache, this is easy with a mod_rewrite directive in your httpd.conf:


RewriteCond %{HTTP_USER_AGENT} *iPhone*
RewriteRule ^topics/index\.html$ iPhone/index.html [L]
I think that is the best solution.

Doesn't the Rewrite Condition have to be RegEx?

RewriteCond %{HTTP_USER_AGENT} .*iPhone.*
 

Lincoln

New Member
Gold
Aug 11, 2007
6,100
4
0
#5
The above mentioned code is what I used for my site back when it was up and running. It worked nicely; you didn't even see the page redirected from when viewing on the iPhone.

- John
 

meiphones_brill

New Member
Silver
Nov 16, 2007
797
0
0
#10
Thank you, but please explain what httpd.conf is. I'm lost!
Griffinaz already gave the answer, but the chances are, by the sound of it, you're going to need to talk to your ISP to make this work.

You probably won't have access to the httpd.conf and what usually happens is you get given the right to upload a file called .htaccess into your web directory

You put the rules in there in stead if this is the case.

Just need to check with your ISP that you can have .htaccess files and that the server has mod_rewrite installed
 

DisneyEcho

New Member
Bronze
Jul 14, 2007
54
0
0
disneyecho.emuck.com
#11
Griffinaz already gave the answer, but the chances are, by the sound of it, you're going to need to talk to your ISP to make this work.

You probably won't have access to the httpd.conf and what usually happens is you get given the right to upload a file called .htaccess into your web directory

You put the rules in there in stead if this is the case.

Just need to check with your ISP that you can have .htaccess files and that the server has mod_rewrite installed
Thanks, guys, yes I handle the forum and its software, but my friend lar3ry handles the computer system the forum runs on and he knows all about that kind of stuff. I appreciate your help!

One more question: If we make that change, that will just be a specific re-direct for that one page, right? That's what I want at this point.
 

cdinca

Member
Bronze
Jul 4, 2007
397
1
16
#12
not to muddy things up, but you could always set force-type or set-handler in the htaccess file...tell apache to treat your topics/index.html as php...then check user agent in a php snip on the page. the benefit of this is that you can check many things if your needs ever change.
 

JoeT

Member
Silver
Dec 18, 2007
911
0
16
Tampa, FL
#13
One more question: If we make that change, that will just be a specific re-direct for that one page, right? That's what I want at this point.

Correct. When you look at the code:

RewriteCond %{HTTP_USER_AGENT} .*iPhone.*
RewriteRule ^topics/index\.html$ iPhone/index.html [L]


The first line tells Apache that the following rule applies when it sees an iPhone making a request.

The second line tells Apache that when the request is for /topics/index.html to send /iPhone/index.html instead. You could alter that line to tell Apache to redirect other lines as well.

The rewrite engine is pretty powerful, and I suggest reading up on it at the Apache website to get an idea of what you can do with it.