homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess redirect mobile to desktop

 12:49 pm on Mar 2, 2013 (gmt 0)

I have a redirect code that I found on this site <snip> and that has been working well for me for a couple of years.

I've been updating my site and would like to have the 'opposite' of this code to redirect visitors using Desktop browsers from my Mobile site to my Desktop site.

(E.g. those who have followed shared links etc. or google searches to arrive at my mobile page directly). I thought that I would be able to simply add an ! to perform the opposite logic on the mobile site, but having so little understanding of what I'm doing, I haven't fluked upon the correct answer.

This is what I have for my redirect from Desktop to Mobile :

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^$ http://mydomain.com/index.html

IndexIgnore *


Any help will be very much appreciated.

[edited by: incrediBILL at 8:48 pm (utc) on Mar 2, 2013]
[edit reason] URL removed, see forum charter [/edit]



 8:03 pm on Mar 2, 2013 (gmt 0)

welcome to WebmasterWorld, grantfs!

my redirect from Desktop to Mobile

in this case, i assume you meant to post this with the "!", correct?

so what response do you actually get when you request http://example.com/ from a non-mobile user agent?

some other issues i noticed:
the RewriteRule as specified will produce a 302 status code.
is this what you want or do you need a 301 here?
if so you will need to specify a [R=301] flag.

i would recommend removing the index.html from the url and use "DirectoryIndex index.html" to specify the default directory index document.
instead you should redirect to the root of that hostname:

get rid of the "RewriteBase /" - it's not doing anything useful for you.

instead of "IndexIgnore *" you would probably be better off with "Options -Indexes"


 2:09 am on Mar 3, 2013 (gmt 0)

my redirect from Desktop to Mobile

in this case, i assume you meant to post this with the "!", correct?

I think he's quoting his existing rule. The ! version would be "from Mobile to Desktop".

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ http://mydomain.com/index.html

The first Condition is redundant, since the Rule itself says the same thing. (Which is good. Never pack something into a Condition if you can put it in the body of a Rule.)

Right now we don't have enough information, though it's possible ::cough-cough:: that our friendly local moderator inadvertently snipped it while trimming away the site-specific details.

What's the difference in URL between the mobile and desktop versions? Different domain, different subdomain, different directory? Or is it all the same URL but you're serving content from different places? (Your post implies this isn't the case, but I always double-check.) This information will be used in two different ways: To determine the form of the final redirect, and to determine where the initial rule needs to be located and possibly what its Conditions are.

What happens to people who come in requesting something other than the front page?

Oh, and your rule isn't only missing the [R=301] flag. It's missing the [L] flag which is potentially far more serious. You might think that R implies L, the way F and G do, but it ain't so. Counter-intuitive, but there you are. Can't argue with Apache.

instead of "IndexIgnore *" you would probably be better off with "Options -Indexes"

Took the words right out of my mouth ;) Not much point in putting your server to the work of producing an index.html file when it's going to end up empty.


 8:46 am on Mar 4, 2013 (gmt 0)

Phranque, Lucy,

thanks for the replies.

Webmaster is something of a misnomer for me, because I am simply a photographer who has been looking after his own website since the late 90s. So please don't expect too much of me. :)

Lucy ... yes absolutely, I was simply quoting the existing rule, which has seemed to be good to me, however I have no idea whether people have issues with this redirect or not.

I have my mobile site as a sub-domain (main site : grantsymon.com - mobile site : mobile.grantsymon.com). The directory structure is not the same, though it could easily be if it makes things easier.

Unfortunately, anyone requesting something other than the front page is not redirected. I would much prefer that they were.


 9:38 am on Mar 4, 2013 (gmt 0)

anyone requesting something other than the front page is not redirected

that is precisely what you are instructing the server to do.

with this line of code, you are restricting the RewriteRule to the root directory (home page):

RewriteCond %{REQUEST_URI} ^/$


 10:44 am on Mar 4, 2013 (gmt 0)

What's your current cookie setup?

Optimally what should happen is: When someone first arrives at the site, they should be sent to the desktop or mobile version based on their UA, unless there's already a cookie expressing a preference. Every page should have a small unobtrusive-- but not invisible --link to the other version. And when your user goes to another page on the site, they should be kept on the same version, desktop or mobile.

It is just as easy to redirect all page requests as it is to redirect index-page alone. You already have-- or should have --the same type of code in place; it's what you use in your domain-name-canonicalization redirect. Essentially you're capturing the whole request and then sending them along to the appropriate (sub)domain.

The directory structure is not the same, though it could easily be if it makes things easier.

Yes, yes, yes. If you can possibly do this, it would make everything enormously easier.

Structurally you need something like this:

RewriteCond {check for cookie and see what it says}
RewriteCond {check whether (sub)domain matches cookie}
RewriteCond {check User-Agent}
RewriteRule {take action}

Unfortunately you can't do if/else loops in mod_rewrite alone. So you either have to make a series of rules covering all possibilities, or take a preliminary detour to a php script that checks everything:

--is cookie present?
--if yes, what does it say?
--which subdomain was requested?

and then:

--if cookie is present, does request agree with its setting?
--if there is no cookie, does request agree with user-agent?

Note that if the cookie is present, you do not need to check the user-agent at all. The user has already made a choice. Happily this is both good for your visitors-- because they're getting what they want --and also good for your server --because it doesn't have to do as much work :)


 12:28 pm on Mar 4, 2013 (gmt 0)


I took out the line
RewriteCond %{REQUEST_URI} ^/$ but it has not made any difference. iOS devices can still access pages further in than the index.


I know that netiquette says that site visitors should not be forced to do anything they may not want to, but in the case of photography, I believe it is a bit different. Just as I would not want to just use the most convenient way to show a potential client a printed portfolio, equally, online I want them to see my images in the best way I can provide. So that means that I want to direct specific devices to the mobile page and all desktop users to the desktop page. From what you have said this will greatly simplify my life, since I won't need to set and subsequently look for cookies.

If I recreate the structure so that the desktop and mobile sites match exactly (which I shall do this evening) what do I need to put in htaccess to redirect to the equivalent/matching pages? Ideally in both directions. Mobile to desktop and desktop to mobile.


 10:23 pm on Mar 4, 2013 (gmt 0)

Well, ###. I had to leave in the middle of composing a post, and was really hoping that in the intervening hours someone would drift by and answer all your questions.

Yes, you can certainly redirect in both directions. It's basically the identical code either way. Except that the user-agent detector will switch between "is" and "is not", and of course the (sub)domain names will change.

Some double-checks:

Does your site use cookies at all? (If no: You don't need to learn a whole new "cookie language". It can be done right in mod_rewrite :))

Will users ever have a choice about which version to use? You and the users may disagree on which version offers the better user experience-- and, of course, you will never be able to list all possible User Agents.

Do you plan to re-check the User Agent on each new page load? The alternative requires either cookies or a referer check, with double confusion if someone voluntarily asks for the other version of the site.

How carved-in-stone is the whole thing? Are they fundamentally different sites, or can the differences be reduced to using alternate stylesheets?

I ask this question because last night I serendipitously rediscovered some utterly brilliant code that SOMEONE ELSE posted here last fall. It uses only html and css and is based strictly on viewport size. The only limitation is that it won't work on browsers that don't recognize the "max-width" property. But according to caniuse dot com,* that's MSIE <= 6, so probably expendable.

* Do not repeat not make the mistake I am constantly making w/r/t this domain name. EEEUW.


 7:36 am on Mar 5, 2013 (gmt 0)


thanks again for the reply.

I have realised that I cannot easily make the site structures identical. I could 2 weeks ago, but now I've changed the way the mobile site works, I can no longer do this. This whole thing is really about improving the experience on iOS devices (and Android, which I get a lot less).

No I don't want to set cookies.
No I don't want users to have the choice of site.
Yes I do want to check User Agent on each visit.

The sites are very different. The mobile is specifically geared towards iOS and uses a different approach to displaying the content.


 11:33 am on Mar 10, 2013 (gmt 0)

Can anyone help me with where I need to insert the '!'. (Polite replies only please).

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved