简体   繁体   English

移动版本的文档根目录(.htaccess?)

[英]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 . 我有一个网上商店,自从Google决定推广网页的移动版本以来,我正在开发m.example.com

Here are the trick. 这是窍门。 I do not want, m. 我不想, 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. 因此, http://example.com/contact/将是台式机用户和移动用户的网址。

The mobile version of the page is under a subdirectory ./mobile/ . 该页面的移动版本位于./mobile/子目录./mobile/

Is it possible somehow to force apache, to change document root, if someone is coming from mobile, but keep the original URLS? 如果有人来自移动设备,但可以保留原始URL,则是否可能以某种方式强制apache,更改文档根目录?

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 ? http://example.com/contact/应该运行/var/apache/example.com/mobile/contact.php ,但对于台式机版本,应该运行/var/apache/example.com/contact.php As I mentioned, the URLs are the same. 如前所述,URL是相同的。

If the question is not clear, please leave a comment. 如果问题不清楚,请发表评论。

Something like this? 像这样吗 (Collated from this answer and then just doing a RewriteRule ) (从此答案中整理,然后仅执行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 信用额必须为原始答案 - 使用htaccess的移动重定向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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