1

I want the same URL website.com serving totally different content based on whether mobile or not.

This means, no CSS media queries, and no specific URL m.website.com.

I don't even know if this is possible. But I think I have seen this on this website (not sure).

In other words, imagine a "mobile" folder and a "desktop" folder on the server. Would it be possible to serve the content of "mobile" folder to the root website.com/ if mobile, and serve desktop if not.

lapin
  • 2,098
  • 2
  • 21
  • 30

1 Answers1

2

You can do it with PHP and the HTTP User Agent:

function find_mobile_browser()
{
  if(preg_match('/(alcatel|android|blackberry|benq|cell|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mobi|motorola|nokia|palm|panasonic|philips|phone|sagem|sharp|smartphone|sony|symbian|t-mobile|up\.browser|up\.link|vodafone|wap|wireless|xda|zte)/i', $_SERVER['HTTP_USER_AGENT']))
  {
    return true;
  }
  return false;
}

Call this Function in the beginning of a code and it returns True if your user uses a Mobile User Agent. Ant then you can include the actual site. The if-Block could look like this: (Im not sure about the Include/Requested URI at the Moment)

if($_SERVER['REQUESTED_URI'] == ''){
  $request = index.html;
}
else{
  $request = $_SERVER['REQUESTED_URI'];
}
if(find_mobile_browsers()){
 include('mobile/'.$request);
}
else{
 include('desktop/'.$request;
}

Be Aware that the User Agent can be changed or doesn't get send to you but in this Case it would just return the Desktop Version.

For the .htaccess see this site: http://jrgns.net/redirect_request_to_index/

j_s_stack
  • 667
  • 1
  • 5
  • 18
  • This may be good for a single page, but how would this allow me to entirely serve the content of `mobile/` folder ? – lapin Jan 27 '15 at 02:37
  • With the "include mobile" edit you just made, I think I understand better, let me check if this works : ) – lapin Jan 27 '15 at 02:39
  • Put this in your Root Dir and Redirect all requests to this script with your .htaccess File. With the requested uri Field in the $_SERVER Array you can open the File that was called – j_s_stack Jan 27 '15 at 02:40
  • After I fixed the typos in function names, I came across an error, so I changed a bit the code to : `include dirname(__FILE__).'/mobile/'.$_SERVER['REQUESTED_URI'];` but I still get quite the same error : `Warning: main(/home/.sites/42/site27/web/mobile/) [function.main]: failed to open stream: No such file or directory in /home/.sites/42/site27/web/index.php`. What is wrong? – lapin Jan 27 '15 at 03:19
  • My root is the `web/` folder. It runs from `index.php` in it. I added `dirname` (absolute path) to try to solve the problem as advised [here](http://stackoverflow.com/questions/13803261/warning-function-include-failed-to-open-stream-no-such-file-or-directory). – lapin Jan 27 '15 at 03:23
  • Why dont u use a relative Link? – j_s_stack Jan 27 '15 at 03:25
  • Because I get the same error : `Warning: main(mobile/) [function.main]: failed to open stream: No such file or directory in /home/.sites/42/site27/web/index.php` (this is what I get with `include 'mobile/'.$_SERVER['REQUESTED_URI'];`) – lapin Jan 27 '15 at 03:26
  • What does it say when u echo `$_SERVER['REQUESTED_URI']`? – j_s_stack Jan 27 '15 at 03:27
  • Nothing (`NULL` with `var_dump`). Because I access the URL like this `website.com` – lapin Jan 27 '15 at 03:32
  • But it works when hard coded with `include 'mobile/index.html';` – lapin Jan 27 '15 at 03:36
  • Ok I found a workaround, I'll post an edit about that, but for your answer to be complete : how do you " Redirect all requests to this script with your .htaccess File" ? – lapin Jan 27 '15 at 04:25
  • Thank you. For the index.html problem, I came up with the exact same solution. For the `.htaccess`, I had to combine what I found in your link with [this one](http://stackoverflow.com/questions/3522921/htaccess-redirect-all-pages-to-single-page) to make it work. But then there is the final problem : all sources (img, css etc) are now broken because the url path has changed from `mobile/` to `root`. Any ideas ? – lapin Jan 27 '15 at 05:25
  • add `mobile/` in front of every URL – j_s_stack Jan 27 '15 at 05:31
  • Yes of course. However, if I think more about that, shouldn't accesses to css files etc also be redirected ? Any way to do it with htaccess? Or I wont be able to work on the project locally with those new hard coded css urls... – lapin Jan 27 '15 at 05:35
  • ?? you can redirect css Files too but why? – j_s_stack Jan 27 '15 at 05:37
  • If I add `mobile/` in front of every URL in the whole website, it will break my code when I work locally, and only work online. Which is quite inconvenient..! – lapin Jan 27 '15 at 05:47
  • I absolutly doesnt understand the Problem Sorry! Please open a new Question with your code etc. – j_s_stack Jan 27 '15 at 05:50
  • Let me explain better. Now we have `website.com/mobile/index.html` displayed when accessing only `website.com`. So the URLs in it such as `css/style.css` are broken, because I guess it looks for it in `website.com/css/style.css` when it is actually in `website.com/mobile/css/style.css` – lapin Jan 27 '15 at 05:52
  • Thats right! What exactly is your htaccess at the Moment?Lets go into Chat => http://chat.stackoverflow.com/rooms/69642/discussion-between-j-s-stack-and-lapin – j_s_stack Jan 27 '15 at 05:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69642/discussion-between-j-s-stack-and-lapin). – j_s_stack Jan 27 '15 at 05:57