Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up nginx to redirect mobile users

Tags:

nginx

mobile

I want my website to redirect mobile users from mydomain.com to m.mydomain.com (unless they have specifically asked to see the non-mobile site).

I was doing this in my application using WURFL, but I want to enable page caching. If page caching is on, the application will never be reached to know to redirect the mobile user, and so I need nginx to do this.

Apache has modules such as Apache Mobile Filter that make use of WURFL.

Is there any good way to detect a mobile browser in nginx? I'd rather not just come up with a user-agent regex since there are so many different mobile user agents that are always changing.

like image 909
travis-146 Avatar asked Jan 29 '10 19:01

travis-146


People also ask

How do I redirect a mobile user?

Add a rel="alternate" tag on the desktop page which points to the corresponding mobile URL. Add a rel="canonical" tag on the mobile page that points to the corresponding desktop URL. This allows Google's crawlers to understand the relationship between the two URLs – desktop and mobile.


1 Answers

I don't believe there is a dedicated nginx core or third-party module for this. Since any module would simply test the user-agent anyway, I can't imagine anyone creating a module for this specific purpose.

However, it should be relatively simple to set-up a check on the user-agent and redirect. You may find it preferable to check for Gecko/IE/webkit/opera explicitly and redirect everything else to your mobile site - that way you're maintaining a smaller ua regex, plus you'd be catering for the mobile safari (iPhone/iPod Touch) by keeping them on your main website. Its then a simple step to special-case for that browser should you wish to.

Furthermore, you should be able to check cookie contents in your nginx config to decide whether to redirect based on the user-agent's preferences.

like image 178
Phillip B Oldham Avatar answered Nov 08 '22 10:11

Phillip B Oldham