【发布时间】:2012-07-09 05:38:28
【问题描述】:
我将很快开始使用我的网站的移动版本,已经为其设置了空间,如下所示:
我在这里找到了解决方案: Best way to redirect mobile devices
^ 这似乎工作得很好,除了当我从我的 iphone4 尝试这个时,它只会在我在 url 中输入 www 时重定向。如果我把它排除在外,我只会得到我的常规站点。
因此,如果用户输入 www.example.com 或 example.com,我需要移动重定向才能工作
我对代码进行了一些修改,几乎可以正常工作。现在它适用于两个版本的网址,但我必须刷新一次才能获得移动网站。显然,我希望重定向在他们第一次访问该站点时起作用。我确定我在这里遗漏了一些东西,让我知道你的想法。顺便说一句,我有 aplus.net 作为我的主机。
RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com) [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ http://mobile.example.com/$1 [L,R=302]
回应乔恩·林...
是的,我的 .htaccess 中还有一些其他规则
定义不同的索引页面类型:
DirectoryIndex index.html index.htm INDEX.HTML INDEX.HTM Index.html Index.htm default.htm Default.htm index.shtml index.cgi index.php index.php3 index.jsp index.phtml
从 url 中删除 .php 扩展名
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
然后这里是从cp中自动放入的:
# [ CP Global Redirection - Start ]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_NAME} !^www\..*$
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{SERVER_NAME} !^www\..*$
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}/$1 [R]
</IfModule>
# [ CP Global Redirection - End ]
【问题讨论】:
-
您的服务器/虚拟主机配置或 htaccess 中是否有任何其他重写规则或重定向设置?
-
显然我不熟悉在这里回复 cmets。我试图从我的 .htaccess 中粘贴其他规则,但看起来字符太多?
-
编辑您的帖子并将其添加到底部
-
非常感谢它已被编辑。
-
我建议你使用基于 Javascript 的重定向,它比用户代理猜测的更强大:github.com/miohtama/detectmobile.js