使用查询字符串的移动设备的mod_rewrite

时间:2012-06-29 06:39:21

标签: apache .htaccess mod-rewrite mobile

我有一个网站,可以让我通过URL中的查询访问该网站的移动版本。

我无法弄清楚如何自动将移动用户重定向到此版本(目前您必须添加'?useformat = mobile'才能查看)。

这是我目前的htaccess设置

RewriteEngine On

#MediaWiki short URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

#Mobile - not working?
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^([^/]*)$ /index.php?useformat=mobile&title=$1 [L]

#HTTPS redirect (site uses HTTPS)
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

有点新的mod_rewrite,以前没用过多少东西。

1 个答案:

答案 0 :(得分:0)

您需要在移动规则上方添加此内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

您需要将移动规则移到mediawiki规则之上,因为它在应用移动规则之前会重写,然后您需要将HTTPS规则移到上面。所以,它应该看起来像这样:

RewriteEngine On

#HTTPS redirect (site uses HTTPS)
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

#Mobile - not working?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^([^/]*)$ /index.php?useformat=mobile&title=$1 [L]

#MediaWiki short URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]