skip to Main Content

I have searched stackoverflow for whole day, found code only working for redirecting Desktop ->Mobile, sometimes it works, sometimes it not:

RewriteCond %{QUERY_STRING} !^desktop
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ https://www.example.com/m/ [L,R=302]

Anyway, i can not find any working solution for redirecting Mobile -> Desktop.

Can anybody share working solution of .htaccess for redirection https://www.example.com —> https://www.example.com/m/ for Mobile devices and https://www.example.com/m/ —> https://www.example.com/ for Desktop users.

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    I just figured out the problem. Found that Cloudflare has mobile redirect option by itself. So made a mobile site on subdomain, enabled mobile redirect on CF to that subdomain. Enabled “Cache everything” on both root domain and subdomain. Removed redirect rules from .htaccess. Now i have normal TTFB and mobile traffic redirection is working. Only Desktop->Mobile - will be good as is it.


  2. You can try these 2 redirect rules for mobile -> desktop redirection:

    RewriteEngine On
    
    RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up.browser|up.link|audiovox"[NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone/|wap1.|wap2.|iPhone|android"[NC]
    RewriteRule ^ - [E=MOBILE:1]
    
    # desktop -> mobile
    RewriteCond %{QUERY_STRING} !^desktop [NC]
    RewriteCond %{ENV:MOBILE} =1
    RewriteRule ^ /m%{REQUEST_URI} [L,NE,R=302]
    
    # mobile -> desktop
    RewriteCond %{QUERY_STRING} ^desktop [NC,OR]
    RewriteCond %{ENV:MOBILE} ^$
    RewriteRule ^m/(.*)$ /$1 [L,NC,NE,R=302]
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search