How to properly implement the "__cf_mob_redir" cookie?

#1

I am trying to implement this “__cf_mob_redir” cookie to avoid mobile redirection of some specific files but so far it doesn’t seem to work. The files get redirected to the mobile subdomain regardless of this cookie. This is my code:

<?php $cookie_name = "__cf_mob_redir"; $cookie_value = "__cf_mob_redir = 0"; $path="/"; $domain=".mydomain.com"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), $path, $domain); ?>;

What am I doing wrong?

0 Likes

“__cf_mob_redir” cookie how set?
“__cf_mob_redir” cookie how set?
#2

Ok I managed to find the solution myself and thought to share it here in case someone else might need it in the future.

It turns out you need to set this “__cf_mob_redir=0” cookie in your “.htaccess” file and not through PHP as I initially tried… Here is the code you need to put in your “.htaccess” file:

Header set Set-Cookie: “__cf_mob_redir=0; path=/folder_to_exclude; domain=.yourdomain.com
Header add Set-Cookie: “__cf_mob_redir=0; path=/another_folder; domain=.yourdomain.com

Now Cloudflare will successfully redirect your site to the mobile subdomain on mobile devices except the folders you excluded in the cookie path which will be displayed from the desktop version of your site!

Hope this helps!

2 Likes

#3

I am trying to implement this “__cf_mob_redir” cookie to avoid mobile redirection of some specific files but so far it doesn’t seem to work. The files get redirected to the mobile subdomain regardless of this cookie. This is my code:

<?php $cookie_name = "__cf_mob_redir"; $cookie_value = "__cf_mob_redir = 0"; $path="/"; $domain=".mydomain.com"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), $path, $domain); ?>

What am I doing wrong?

0 Likes

#4

Ok I managed to find the solution myself and thought to share it here in case someone else might need it in the future.

It turns out you need to set this “__cf_mob_redir=0” cookie in your “.htaccess” file and not through PHP as I initially tried… Here is the code you need to put in your “.htaccess” file:

Header set Set-Cookie: “__cf_mob_redir=0; path=/folder_to_exclude; domain=.yourdomain.com
Header add Set-Cookie: “__cf_mob_redir=0; path=/another_folder; domain=.yourdomain.com

Now Cloudflare will successfully redirect your site to the mobile subdomain on mobile devices except the folders you excluded in the cookie path which will be displayed from the desktop version of your site!

Hope this helps!

1 Like

closed #5
0 Likes

#6

I am trying to implement this “__cf_mob_redir” cookie to avoid mobile redirection of some specific files but so far it doesn’t seem to work. The files get redirected to the mobile subdomain regardless of this cookie. This is my code:

<?php $cookie_name = "__cf_mob_redir"; $cookie_value = "__cf_mob_redir = 0"; $path="/"; $domain=".mydomain.com"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), $path, $domain); ?>

What am I doing wrong?

0 Likes