简体   繁体   中英

.htaccess code for Desktop ->Mobile | Mobile -> Desktop?

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.

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM