Best .htaccess rule for desktop -> desktop redirect?


Hello Cloudflare & community.

This is my first post and first of all I want to say one big THANK YOU, for existing, as a service, and giving almost anyone the chance for big big growth.

Recently I’ve spent hours and hours in the Cloudflare’s admin area, and gradually managed to tune up my sites the way they should be.

There’s only one question unanswered, which bothers me - how to, best, redirect Desktop users, if, by chance, they visit the mobile subdomain, that is, of my site? I want to redirect them back to

What I’ve done - I’ve stated in the mobile theme header the canonical url (my site uses Wordpress). Also - I’m using a simple javascript -

<script type="text/javascript">
if (screen.width >= 1024) {
document.location = "<?php echo str_replace( home_url(), '', get_permalink($post->ID) ); ?>";
  • that is, having in mind, that almost all mobile devices have screen width lesser than 1024 pixels.

Still, some infinite loops occur now and then, although it may be only once an hour or two or so.

I know, that with Enterprise plan one would get separate caching for mobile, tablets and desktop. But yet, that would still not work completely - as here the case is, I wanna redirect all user agents, which are not send via Cloudflare to, yet have visited, back to , as well as keep the request URI.

I would be very grateful, if you could provide me a sample htaccess directive, I could put before/after the Wordpress rules, and make everything tip-top with my sites. The javascript works like 90% of the time, but seems unprofessional to me and really bothers me and keeps me reading and reading :slight_smile:

Thank you in advance, if you’ve got any idea!


Looking at my script that does the reverse (see below), perhaps the str_replace has something to do with it. Nobody else has responded, so I’m just guessing here. I have’t heard reports of an infinite loop.

<script type="text/javascript">
if (screen.width <= 800) {
document.location = "";


The only problem is, that Cloudflare declares some devices with bigger screen width as mobile. So when we try to move them to desktop version, Clodflare sends them back to mobile and that process is endless. That’s exactly what my question is for. We should not use screen width, but agents in an htaccess file…


Oh, so this is coupled with the Mobile Redirect option in Cloudflare’s Speed tab? If so, then I suppose you’ll have to turn off that feature and handle all redirects at your end. Page Rules don’t seem to have an option for this feature, and I can’t think of any other way to break your redirect loop.


Yeah, finally I found a better decision - used

<script type="text/javascript">
 if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
document.location = "";

in header of desktop theme, and

 <script type="text/javascript">
if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 document.location = "";

in header of mobile theme.

But I find it illogical to cater only for one way round… I think Cloudflare should provide that feature with the opposite case - at least that’s what logic says.


You’ve come across a good idea for a Cloudflare App;
it appears you’ve coded most of the app already.

I agree offering a mobile redirect only to end users is an outdated feature it’s 2018. Mobile redirects were like so 2016.
[ insert 1rem of humor ]

Though, seriously I would be interested, if you created a client specified (user agent) redirect CF_app.

e.g., We wasted 3 hours yesterday rebuilding 20 pages
for desktop only just for the owners because they only view our website with a desktop.

A month of last year wasted which has pushed us back four months behind last year’s schedule.