The ultimate mobile .htaccess redirect

I have a decent working solution for redirecting mobile / desktop computers using the .htaccess code below, but there are a few more improvements that can really make it final if someone can help.

Directory setup:

  • desktop site: website.com
  • mobile site: m.website.com

Cookie Links:

  • will go from the desktop to the mobile site, set the cookie on the mobile: http://m.website.com?m=1
  • switching from a mobile site to the desktop, setting a cookie to work on the desktop: http://www.website.com?m=0

Current .htaccess file:

RewriteEngine on # Check if this is the desktop to mobile query string RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) # Set a cookie, and skip the next 2 rules RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=2] # Check if this is the mobile to desktop query string RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) # Set a cookie, and skip the next rule RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S] RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] # Check if we're not already on the mobile site RewriteCond %{HTTP_HOST} !^m\. # Can not read and write cookie in same request, must duplicate condition RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) # Check to make sure we haven't set the cookie before RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC] RewriteRule ^(.*)$ http://m.website.com/ [L,R=302] 

Things I want to add:

  1. If we switch to the desktop version, either from a mobile or just from a regular desktop, make sure that it always adds www.
  2. To redirect from the desktop to the mobile, directly to the corresponding mobile page, but if it does not exist by default for the mobile home. (Example: if you visit website.com/chairs on your mobile device, it will try to redirect to m.website.com/chairs, but if it does not exist, it will be redirected to m.website.com. Something like ^ (. *) $ http: //m.website.com/$1, but not sure how to implement the backup on the home page.

Also, if anyone notices any bugs or improvements in the current code, this will also be great!

+6
source share
2 answers
 RewriteEngine on # Check if this is the desktop to mobile query string RewriteCond %{QUERY_STRING} (^|&)m=1(&|$) # Set a cookie, and skip the next 2 rules RewriteRule ^ - [CO=mredir:1:%{HTTP_HOST},S=2] # Check if this is the mobile to desktop query string RewriteCond %{QUERY_STRING} (^|&)m=0(&|$) # Set a cookie, and skip the next rule RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S] RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] # Check if we're not already on the mobile site RewriteCond %{HTTP_HOST} !^m\. # Can not read and write cookie in same request, must duplicate condition RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) # Check to make sure we haven't set the cookie before RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC] RewriteRule ^(.*)$ http://m.website.com/ [L,R=302] # 1.) Check if on desktop site and having www., if not redirect to www.website.com RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} !^m\. [NC] RewriteRule ^(.*)$ http://www.website.com$1 [R=301,L] 

2.) this cannot be done with mod_rewrite, except that "/ chairs" is an existing file (and, I think, it is not). So I would test it with your desired scripting language - for example. in php:

 if (!pageexists($_GET['page']) && $_SERVER['HTTP_HOST'] == 'm.website.com') header("Location: http://m.website.com/"); 

where pageexists () is the function that you define to check if there is content for this page in the mobile version.

0
source

RewriteEngine On RewriteBase /

RewriteCond% {HTTP_USER_AGENT} (android | bb \ d + | meego). + mobile | avantgo | bada / | blackberry | blazer | compal | elaine | fennec | hiptop | iemobile | ip (hone | od) | iris | kindle | | | | | | | | | | | | ) 0 | symbian | treo | up. (Browser | link) | vodafone | wap | windows \ (ce | phone) | xda | xiino [NC, OR] RewriteCond% {HTTP_USER_AGENT} ^ (1207 | 6310 | 6590 | 3gso | 4thp | 50 [1-6] i | 770s | 802s | a \ wa | abac | ac (er | oo | s-) | ai (ko | rn) | al (av | ca | co) | amoi | an (ex | ny | yw) | aptu | ar (ch | go) | as (te | us) | attw | au (di | -m | r \ | s \) | avan | be (ck | ll | nq) | bi (lb | rd) | bl (ac | az) | br (e | v) w | bumb | bw- (n | u) | c55 / | chapi | CCWA | CDM- | cell | chtm | CLDC | Cmd- | co (three | th) | goiter | yes (this | LL | ng) | dbte | DCs | devi | DICA | DMOB | do (c | p) O | DS (12 | -d) | el (49 | ar) | em (l2 | ΞΌl) | e (IC | k0) | esl8 | ez ([4-7] 0 | basis | wa | ge) | fetc | fly (- |) | g1 \ u | g560 | gene | gf-5 | g-mo | go (.w | od) | gr (ad | un) | haie | hcit | hd- (m | p | t) | hei- | hi (pt | ta) | hp (\ i | ip) | hs-c | ht (c (- | \ || a | g | p | s | t ) | tp) | hu (aw | tc) | i- (20 | go | ma) | i230 | iac (\ | - | /) | ibro | idea | ig01 | ikom | im1k | inno | ipaq | iris | ja ( t | v) a | jbro | jemu | jigs | kddi | keji | kgt (\ | /) | klon | kpt \ | kwc- | kyo (c | k) | le (no | xi) | lg (\ g | / (k | l | and) | 50 | 54 | - []) | libw | lynx | m1-w | m3ga | m50 / | ma (t | u | xo) | tf (01 | 21 | ca) | m-cr | me (rc | ri) | mi (o8 | oa | ts) | mmef | mo (01 | 02 | bi | de | do | t (- | \ | o | v) | zz) | mt (50 | p1 | v \) | mwbp | mywa | n10 [0-2] | n20 [2-3] | n30 (0 | 2) | n50 (0 | 2 | 5) | n7 (0 (0 | 1) | 10) | ne ((s | m) - | on | TF | WF | WG | weight) | knock (6 | i) | nzph | o2im | op (Ti | WV) | Oran | ow g1 | p800 | pan (a | d | t) | pdxg | pg (13 | - ([1-8] | s)) | phil | Pire | pl (ay | yak) | p-2 | ro (ck | ct | ce) | Prox | PSIO | PT-g | QA | ds (07 | 12 | 21 | 32 | 60 | - [2-7] | I-) | Qtek | R380 | R600 | rax | rim9 | ro (ve | zo) | S55 / | sa (GE | ma | mm | ms | pu | va) | RLS (01 | H- | oo | p) | SDK / | yourself (s (- | 0 | 1) | 47 | tf | d | n) | sgh- | ball | Sie (- | m) | ck-0 | cl (45 | ID) | cm (al | ar | b3 | he | t5) | so (ft | ny) | sp (01 | h- | v- | v \) | sy (01 | mb) | t2 (18 | 50) | t6 (00 | 10 | 18) | ta (gt | lk) | tcl- | tdg- | body (i | m) | countdown | t-mo | to (mn | Sh) | q (70 | m- | m3 | m5) | TY-9 | up (.b | g1 | si) | utst | V400 | V750 | Believe | VI (gk | te) | VK (40 | 5 [0-3] | -v) | vm40 | Voda | vulc | th (52 | 53 | 60 | 61 | 70 | 80 | 81 | 83 | 85 | 98) | w3c (- | \) | webc | whit | wi (g \ | nc | nw) | wmlb | wonu | x700 | yas- | your | zeto | zte-) [NC] RewriteRule ^ $ http://detectmobilebrowser.com/mobile [R, L]

0
source

Source: https://habr.com/ru/post/902270/


All Articles