仅当存在移动页面时,如何使用 apache 将用户重定向(基于 user_agent 并使用 mod_rewrite)到我的移动网站。

仅当存在移动页面时,如何使用 apache 将用户重定向(基于 user_agent 并使用 mod_rewrite)到我的移动网站。

我想根据 user_agent 检测移动用户。

如果用户正在浏览网站的完整版本并且他们具有特定的 user_agent,我想将他们重定向(使用 mod_rewrite)到移动网站 - 如果移动页面存在。

完整网站 URL 示例 http://www.domain.com/page1.html 等效移动网站 URL 示例(请注意,使用 .htm 而不是 .html) http://m.domain.com/page1.htm

我需要用 apache 来做这个,而不是用 php 之类的

奖励:移动网站可以链接到完整网站(如果页面没有移动等效项),并且我想继续重定向人们,并且我想继续重定向,除非人们点击“完整网站”链接,这将通过 url 参数选择退出重定向 - 例如:?ver = ful

答案1

我要刺一刺。

在完整站点虚拟主机中:

RewriteCond %{HTTP­_US­ER_­AGENT} Some-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} Some-Other-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} Yet-Another-Mobile-UA [OR]
RewriteCond %{HTTP­_US­ER_­AGENT} The-Final-Mobile-UA
RewriteCond /var/www/mobile%{REQU­EST­_URI} -f
RewriteCond %{HTTP­_CO­OKIE} !stayOnFullSite
RewriteRule /(.*)l http://m.domain.com/$1 [R]

在移动网站虚拟主机中:

RewriteCond %{QUER­Y_S­TRING} ver=full
RewriteRule ^ - [CO=stayOnFullSite:true]

RewriteCond %{REQU­EST­_FI­LENAME} !-f
RewriteCond /var/www/html%{REQU­EST­_URI}l -f
RewriteRule /(.*) http://www.domain.com/$1l [R]

笔记:

.html当重定向到移动网站时,我相当粗鲁地将结尾的“l”切掉,并在重定向回来时将其重新添加。

我在检查主站点上是否存在该文件时也将其删除,但我不知道如何在检查移动站点上是否存在该文件时将其删除。可能在它前面有另一个 RewriteCond,它匹配除最后一个“l”之外的所有内容%{REQUEST_URI},然后%1在以下 RewriteCond 中使用,如下所示:

RewriteCond %{REQU­EST­_URI} (.*)l
RewriteCond /var/www/mobile%1 -f

使用单一文件扩展名来标准化您的网站可能会更容易。

您可以设置其余可用的 cookie 参数。详细信息请参阅mod_rewrite 文档旗帜部分

此配置完全未经测试。

答案2

Apache 移动过滤器是免费的,易于安装和配置。以下是示例:

#Configuration AMF Filter
#
PerlSetEnv AMFMobileHome /usr/local/AMF
PerlSetEnv AMFProductionMode true
PerlSetEnv ServerMemCached localhost:11211

PerlTransHandler +Apache2::AMFLiteDetectionFilter
RewriteEngine on
RewriteCond %{ENV:AMF_DEVICE_IS_MOBILE} ^true*
RewriteRule ^(.*)$ http://m.foo.org [R=301,L]

让我知道

相关内容