Welcome to WebmasterWorld Guest from

Forum Moderators: ergophobe

Message Too Old, No Replies

Mobile Bidirection Redirect

Source for Mobile Bidirection Redirect



1:13 pm on May 3, 2013 (gmt 0)

Does anyone know of a place where I can find a mobile bidirection/bidirectional redirect? According to this document:


Google is fine with the use of this form of redirect but I'm having a difficult time finding a resource that lists this sort of redirect.

There doesn't seem to be any issue with finding a multitude of unidirectional redirects but I'm trying to provide the best user experience for my desktop users in the event they are directed to a mobile web page.


4:34 pm on May 3, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

# Default Template
SetEnvIf Remote_Addr . my_template_file=default

# Change template based on visitor's browser
SetEnvIf User-Agent \biPad\b my_template_file=iPad
SetEnvIf X-WAP-Profile \bNN70\b my_template_file=small-screen

# Allow visitor to override template selection via cookie
SetEnvIf Cookie \bmy_template_file=([^;]+); my_template_file=$1

# Redirect user-agent to selected template
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /cgi-bin/%{ENV:my_template_file}.pl [L]

This is a simple example of a technique I devised to redirect various user-agents in my custom CMS. It allows for an unlimited number of template files and only one redirect (internal) is required. This type of redirection should be used with the Vary: User-Agent header.

I don't know what type of CMS you use, but maybe you can modify this example code for your own use.


5:05 pm on May 3, 2013 (gmt 0)

Thanks for this. To clarify, in my particular situation, I have two URLs. I have a mobile URL and a desktop URL. With your script, if I'm referred to the mobile URL using a desktop computer, will I be automatically redirected to the desktop version?

Thanks in advance.


5:50 pm on May 3, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Hard to say with the limited information you've provided. It sounds like you've developed two entirely separate sites for your mobile and desktop users. Is that content generated dynamically or is it all static? Need more information.

I have a mobile url and desktop url too. They just happen to be identical (hence the need for the Vary: User-Agent header). In my CMS, there is only one package module processing the various template files. If you're using a single cgi script to dynamically generate both the desktop and mobile pages, then the example code could probably be configured to work on your site. If not, you probably just need a simple 301 or 302 redirection to send mobile users to the mobile version of your site and vice versa for your desktop vistors.


7:04 pm on May 3, 2013 (gmt 0)

You're right. I'm developing 2 separate URLs. The desktop version would be the this for example:


The mobile version would be this:


Currently, the site consists of static web pages and I'm not using a CMS at this point. (That'll change in about 8 months though) Do you know of the bidirectional script I would need or do you know of a place where I can find the exact script I need? Would you happen to know of of an example of the 301 redirection script you refer to and would you know of where I can find a bidirectional 301 redirection script? Sorry if I'm botching the information but as you can tell, I'm not technical.

Interesting that you bring up the Vary HTTP header. I have another thread going on this subject here and there seems to be some confusion on exactly what to do with it.

In your opinion, is the Vary HTTP header required for my particular situation? If so, exactly where do I place this Vary HTTP header and exactly what would the script look like?

According to Google's documentation, they indicate this:

Please note that if your site automatically redirects mobile visitors coming to the desktop site to the mobile site, or vice versa, please be sure to configure your server to include the Vary HTTP header


Do you have an opinion on this based upon your experience?

Much appreciated.


8:30 pm on May 3, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

# Redirect mobile devices to mobile url
# Add as many attributes/values as you like...
SetEnvIf User-Agent \b[Mm]obile\b mobile
SetEnvIf X-WAP-Profile . mobile

RewriteCond %{ENV:mobile} ^1$
RewriteCond %{REQUEST_URI} !^/mobile/
RewriteRule (.*) http://www.example.com/mobile/$1 [R=301,L]

RewriteCond %{ENV:mobile} !^1$
RewriteRule ^mobile/(.*) http://www.example.com/$1 [R=301,L]

I think this code would probably work in your case. Yes, you need to use the Vary: User-Agent header. Also check out the following Google annotations for desktop and mobile URLs:



9:01 pm on May 3, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

There's more than one way to do it, so you have to make decisions. Are you using subdomains (mobile.example.com) or a subdirectory (www.example.com/mobile/)? Where will the redirect happen? javascript, php-or-equivalent, server? If you've currently got static HTML pages, you're probably looking at a server-side redirect. Apache or That Other Server? If Apache, do you have a preferred module? Code involving mod_setenvif was posted above. If you like mod_rewrite, one pattern is

RewriteCond %{HTTP_USER_AGENT} {here you list the likely UAs, such as iPod/iPhone}
RewriteCond %{REQUEST_URI} !^/mobile
RewriteRule (.*) http://www.example.com/mobile/$1 [R=301,L]

RewriteCond %{HTTP_USER_AGENT} !{same list as before, but this time negative}
RewriteRule ^mobile/(.*) http://www.example.com/$1 [R=301,L]

Again, this is only one possible pattern. And I just typed it in off the top of my head. If you're using subdomains, you'd look at %{HTTP_HOST} instead of %{REQUEST_URI}. Since the pair of rules are mutually exclusive (either A or not-A), list them in order of likelihood, based on actual pattern of visits on your own site. Within each rule, if there is more than one condition, list them in order of likely-to-fail.

In addition to the listed conditions, each version should have a supplementary RewriteCond looking at %{HTTP_COOKIE} so you're not redirecting users who have explicitly asked to use the "wrong" version of the site-- or who have been mis-identified by your code.


6:25 pm on May 6, 2013 (gmt 0)

Hi there. Thanks to both of you for your very helpful answers. I really appreciate it.

@lucy: I'm using a subdirectory. As I understand it, we're going to be using a javascript method to redirect the user.

I'm not technical so I have no idea if an Apache or server side redirect could be used. I don't know what a preferred module is but I'lll try to find out.

Please forgive my ignorance but I have two questions:

- for the non technical person, exactly where would these redirection scripts go? Would they go in the head of the HTML of every page?

- Based upon the fact that my site is mounted on a dedicated server. Would either of you know exactly what the code would look like for the vary HTTP header and more importantly, where would it go?

I have researched the vary HTTP header that is listed on the Google's developer page but most people I've spoken to (that are reasonably technical) can't verify if I truly need it or what the exact code should be for my website let alone where it goes.

Any additional help you can provide would be greatly appreciated. Thanks in advance.

p.s. Would either of you know of someone or a firm that can be contacted to help me with this?

Featured Threads

Hot Threads This Week

Hot Threads This Month