.htaccess redirect for blackberry not working
I am trying to redirect blackberry users to my mobile site by doing:
# redirect for blackberry users
RewriteCond %{HTTP_USER_AGENT} ^.BlackBerry.$
RewriteRule ^(.*)$ http://www.domain.com/m/ [R=301]
in my .htaccess, but nothing happens when I try to access from the device, I've alr开发者_运维知识库eady deleted cache and cookis and nothing works. I have been googling around and it seems I'm doing the redirect correctly but I guess not, what am I missing?
My .htaccess only contains that by the way.
Edit The .htaccess in my server's root.
If this isn't the only rule in your .htaccess file, you might have an issue where a later rule messes up your redirect. To redirect immediately, you need to include the L
flag.
I also suspect that your regular expression for the user agent is probably not correct for the input you're testing against, since the two .
match just one character on either side of the word "BlackBerry". It would also be a good idea to guard against a redirect loop with a check to see if you're already in /m/
(although if you have mod_rewrite directives in a .htaccess file in that directory it's not important).
Putting all of that together, we get something like the following:
# Check for x-wap-profile/Profile headers
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile} !^$ [OR]
# Check for BlackBerry anywhere in the user agent string
RewriteCond %{HTTP_USER_AGENT} BlackBerry [NC]
# Make sure we're not in /m/ already
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^ http://example.com/m/ [R=301,L]
You may also want that RewriteRule
to be...
RewriteRule ^.*$ http://example.com/m/$0 [R=301,L]
...if the content is named the same (but mobile-friendly) in the /m/
directory.
Make sure your BlackBerry is not in "emulation mode" where it passes the user-agent for IE or Firefox instead of BlackBerry. You can check in the browser's option screen.
A better way around this would be to base your rewrite rule on the "x-wap-profile" and/or "profile" headers, which the mobile browser should always send accurately.
精彩评论