0

I have a webshop, and since google decided to promote mobile versions of webpages, I am developing my m.example.com.

Here are the trick. I do not want, m. domain. What I want is, when a user is coming, I check the user agent, and if it is coming from mobile, I will show the mobile version, if come from desktop, show the desktop version.

But, I want to use exactly the same urls to the users. So, the http://example.com/contact/ will be the url for both desktop users, and both mobile users.

The mobile version of the page is under a subdirectory ./mobile/.

Is it possible somehow to force apache, to change document root, if someone is coming from mobile, but keep the original URLS?

For example:

http://example.com/contact/ should run /var/apache/example.com/mobile/contact.php, but for desktop version, should run /var/apache/example.com/contact.php? As I mentioned, the URLs are the same.

If the question is not clear, please leave a comment.

vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 1
    **Note:** Depending on your application design, this can be done when you load the view (in an MVC design) rather than having 2 projects (1 for desktop, 1 for mobile) – ʰᵈˑ Apr 23 '15 at 08:53
  • Yes, this is an MVC project. But the mobile version, is a new development, in MVC, while the desktop version is a legacy code. – vaso123 Apr 23 '15 at 08:57
  • Another note: it might be easier to achieve this with your PHP application itself, or alternatively adjust your current CSS to be responsive – scrowler Apr 23 '15 at 08:57
  • Responsive design can not be achived. This is the scenario. – vaso123 Apr 23 '15 at 08:57

1 Answers1

0

Something like this? (Collated from this answer and then just doing a RewriteRule)

RewriteEngine On

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
#  had to check, but I believe most mobile devices should send at
#  least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^(.*)$  mobile/$1 [R,L]

Credit must go to the original answer - Mobile Redirect using htaccess

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
  • I do not want to redirect the user, to `m.` I do not want an `m.` version of site. What I want is, apache serve the `www` from `/etc/apache/example.com/mobile` directory. – vaso123 Apr 23 '15 at 09:12
  • That's what the `RewriteRule` at the bottom is for (redirects requests to the `mobile/` directory). The `"Check if we're not already on the mobile site"` was just from the answer linked. – ʰᵈˑ Apr 23 '15 at 09:22
  • Maybe, I do not explain clearly. Let me clarify. I do not want `RewriteRule`. I do not want redirect. What I want is to tell Apache, to get the files from under /mobile/ directory, but do not want to redirect. There are no `m.` site. Check this one: http://bookline.hu – vaso123 Apr 23 '15 at 09:30
  • Perhaps I used the wrong term. The `RewriteRule` won't "redirect", but will (essentially) set the `DocumentRoot` to `mobile/`. So `site.com/index.php` will become `site.com/mobile/index.php`. (though it won't have the `mobile` part in the URL. – ʰᵈˑ Apr 23 '15 at 09:32
  • Ok, I see. Thank you. But anyhow, it is not works for me. I've made a test on a production site. There are an `index.php`, and an `/mobile/index.php`, copied it to the `.htaccess` but if I see it from mobile, it not taking me to the `/mobile/index.php`, it shows me the desktop version. – vaso123 Apr 23 '15 at 10:11