简体   繁体   中英

Document root for mobile version (.htaccess?)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM