Support » Everything else WordPress » How to redirect links for mobile using htaccess?

  • Resolved Sabrina Morgan

    (@sabrinamorgan)


    Hi,

    I’m developing a mobile version of an existing site and need to forward mobile visitors entering from any given link to its equivalent on my mobile site. Example: Sending a mobile visitor to mysite.com/about to m.mysite.com/about.

    I know the best way to do this sitewide is using mod_rewrite, but I’m new to regular expressions. What’s the best way to accomplish this in .htaccess?

    This is what I’ve cobbled together so far. It’s not working right – the main url redirects to the mobile subdomain, but deep links still aren’t redirecting (goal of the last line). What did I do wrong?

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    RewriteEngine on
    RewriteBase /
    
    # prevent looping
    RewriteCond %{HTTP_HOST} !^m.mysite.com$
    
    # if the browser accepts these mime-types, it's definitely mobile, or pretending to be
    RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
    
    # a bunch of user agent tests
    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]
    
    # rewrite rules here
    RewriteRule .? http://m.mysite.com%{REQUEST_URI}  [L,R=302]
Viewing 9 replies - 1 through 9 (of 9 total)
  • Got it working! Here’s the code, in case anyone else runs into this issue:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    
    # prevent looping
    RewriteCond %{HTTP_HOST} !^m.mysite.com$
    
    # if the browser accepts these mime-types, it's definitely mobile, or pretending to be
    RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
    
    # a bunch of user agent tests
    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]
    
    # rewrite rules here
    RewriteRule ^(.+)\$ http://m.mysite.com/$1 [R=302,NC]

    Wow! Big thanks for this.

    Is this for redirecting users when they first hit your site to a mobile site or for links?

    Looks like it’d redirect anyone on a mobile browser to your mobile site.

    I don’t know what you mean by ‘for links’

    if this is to redirect to my mobile site and i have a custom home page, does this get added to my custom home page?

    If you add this code to your .htaccess ALL traffic to ALL pages on your site will be redirected for mobile users.

    how can i get only mobile users to be redirected?

    Well you can use the code up there or try a plugin like WPTouch.

    I tried WPTouch, but that is only for touch screen phones, correct?
    what if the user doesn’t have a touch screen phone? I’d have to develop another mobile site for that case?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to redirect links for mobile using htaccess?’ is closed to new replies.