views:

75

answers:

2

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 already 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.

+2  A: 

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.

Tim Stone
even after I added that and tried on my blackberry it won't work, it's the only rule on my .htaccess so it shouldn't be messing up with anything =/ is there another reason it might not be working? I had never done this before to be honest
Luis Armando
@Luis Armando: Hmm...Just to be sure, you have `RewriteEngine On` in there too right? I'm not seeing anything else that might be going wrong right now, unless for whatever reason the `HTTP_USER_AGENT` is set to something weird, but that doesn't seem likely. I'll throw it on my test server later and see if my eyes are just missing something obvious though.
Tim Stone
missing RewriteEngine On ... I feel so stupid :(, but thanks a lot :)
Luis Armando
@Luis Armando: Ah, alright. No problem! I've also updated the answer to take into account what [Marc mentioned](http://stackoverflow.com/questions/3748325/3748870#3748870).
Tim Stone
You might want to also include RewriteCond %{REQUEST_FILENAME} !-f at the beginning of the rule if you are trying to link to file (css, img, etc) outside of the /m/ directory. This will prevent the rule from applying to real files. Was banging my head till I realized that was the problem.
Louis W
+1  A: 

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.

Marc Novakowski
how dow I do the rewrite on the x-wap-profile? just instead of Blackberry?
Luis Armando