【问题标题】:Document root for mobile version (.htaccess?)移动版本的文档根目录 (.htaccess?)
【发布时间】:2015-04-23 08:50:24
【问题描述】:

我有一个网上商店,由于谷歌决定推广网页的移动版本,我正在开发我的m.example.com

这里有诀窍。我不想,m. 域。我想要的是,当用户来时,我检查用户代理,如果它来自移动,我将显示移动版本,如果来自桌面,则显示桌面版本。

但是,我想对用户使用完全相同的网址。因此,http://example.com/contact/ 将是桌面用户和移动用户的 URL。

移动版页面位于./mobile/子目录下。

如果有人来自移动设备,是否有可能以某种方式强制 apache 更改文档根目录,但保留原始 URL?

例如:

http://example.com/contact/ 应该运行/var/apache/example.com/mobile/contact.php,但是对于桌面版本,应该运行/var/apache/example.com/contact.php?正如我所提到的,URL 是相同的。

如果问题不清楚,请发表评论。

【问题讨论】:

  • 注意:根据您的应用程序设计,这可以在您加载视图(在 MVC 设计中)而不是拥有 2 个项目(1 个用于桌面,1 个用于移动)
  • 是的,这是一个 MVC 项目。但移动版是 MVC 中的新开发,而桌面版是遗留代码。
  • 另一个注意事项:使用您的 PHP 应用程序本身可能更容易实现这一点,或者调整您当前的 CSS 以使其具有响应性
  • 无法实现响应式设计。这是场景。

标签: php apache .htaccess url redirect


【解决方案1】:

这样的? (从this answer 整理,然后只是做一个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]

功劳必归原答案 - Mobile Redirect using htaccess

【讨论】:

  • 我不想将用户重定向到m. 我不想要m. 版本的网站。我想要的是,apache 服务于来自/etc/apache/example.com/mobile 目录的www
  • 这就是底部的RewriteRule 的用途(将请求重定向到mobile/ 目录)。 "Check if we're not already on the mobile site" 只是来自链接的答案。
  • 可能,我解释的不是很清楚。让我澄清一下。我不想要RewriteRule。我不想重定向。我想要告诉 Apache,从 /mobile/ 目录下获取文件,但不想重定向。没有m. 站点。检查这个:bookline.hu
  • 也许我用错了术语。 RewriteRule 不会“重定向”,但会(基本上)将DocumentRoot 设置为mobile/。所以site.com/index.php 会变成site.com/mobile/index.php。 (虽然它不会在 URL 中包含 mobile 部分。
  • 好的,我明白了。谢谢你。但无论如何,它对我不起作用。我已经在生产站点上进行了测试。有一个index.php 和一个/mobile/index.php,将其复制到.htaccess,但如果我从手机上看到它,它不会带我到/mobile/index.php,它显示的是桌面版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多