0

I have written some PHP that detects whether a user is visiting my blog posts using a mobile device or a computer.

Basically, I need a script that edits the URL dynamically if a user is visiting my blog post with a mobile.

For example, my blog post URLs are like this:

http://www.example.com/blog/blog-post-1/
http://www.example.com/blog/welcome-to-my-blog/
http://www.example.com/blog/another-blog-post/

Users should be dynamically redirected to:

http://www.example.com/m/blog/blog-post-1/
http://www.example.com/m/blog/welcome-to-my-blog/
http://www.example.com/m/blog/another-blog-post/

I also have the device detection working in PHP, so I just need the script to insert inside the PHP condition.

Any ideas?

2
  • this should be tagged as a php question, and possibly an htaccess question, but not a javascript or jquery question. Commented Nov 16, 2012 at 15:53
  • OK, sorry, I did not know the best method of doing it Commented Nov 16, 2012 at 15:57

4 Answers 4

2

Try:

if ($mobile) {
    header("Location: http://".$_SERVER["HTTP_HOST"]."/m".$_SERVER["REQUEST_URI"]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

This .htaccess snippet might help to accomplish what you're looking to do:

This rewrite covers most mobile devices (with few false positives). When a mobile device is detected it is redirected to /m/.

http://snipplr.com/view/44741/

Comments

0

If you're already using PHP to do device detection, this should do it:

header("Location: /m{$_SERVER['REQUEST_URI']}");
exit;

It takes the current path and prefixes it with "/m" to form the mobile path.

Using JavaScript to do the redirection would exclude certain mobile devices, so that's probably not the best idea.

Comments

0

This should do it:

$server

<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

user_agent.php:

<?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://mobile.site.com/');
//OR
echo "<script>window.location='http://mobile.site.com'</script>";
}
?>

Index.php:

<?php
include('user_agent.php'); // Redirecting http://mobile.site.info
// site.com data
?>

Content from this site.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.