Note that there are some explanatory texts on larger screens.

plurals
  1. POUltimate mobile .htaccess redirect
    primarykey
    data
    text
    <p>I've got a decent working solution to mobile/desktop redirection in place using .htaccess code below, but there are a few more enhancements that could really make it ultimate if anyone can help.</p> <p>Directory setup:</p> <ul><li>desktop site: website.com</li> <li>mobile site: m.website.com</li> </ul> <p>Cookie enabled links:</p> <ul><li>going from desktop site to mobile site, set cookie to stay on mobile: http://m.website.com?m=1</li> <li>going from mobile site to desktop site, set cookie to stay on desktop: http://www.website.com?m=0</li> </ul> <p>Current .htaccess file:</p> <pre><code>RewriteEngine on # Check if this is the desktop to mobile query string RewriteCond %{QUERY_STRING} (^|&amp;)m=1(&amp;|$) # 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} (^|&amp;)m=0(&amp;|$) # 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} !(^|&amp;)m=0(&amp;|$) # 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] </code></pre> <p>Things I'm looking to add:</p> <ol><li>If we're going to desktop version, either from mobile or just a normal desktop visit, make sure it always adds www.</li> <li>For desktop to mobile redirection, direct to matching mobile page, but if it doesn't exist default to mobile home. (Example: if you visit website.com/chairs on your mobile it will try to redirect to m.website.com/chairs, but if it doesn't exist it will instead redirect to m.website.com. I think mapping would be something like ^(.*)$ http://m.website.com/$1, but not sure how to implement fallback to home page</li> </ol> <p>Also, if anyone notices any errors or improvements in the current code would love to hear that as well!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload