WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to redirect links for mobile using htaccess? (10 posts)

  1. Sabrina Morgan
    Member
    Posted 3 years ago #

    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]
  2. Sabrina Morgan
    Member
    Posted 3 years ago #

    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]
  3. junixblog
    Member
    Posted 3 years ago #

    Wow! Big thanks for this.

  4. iluvwrdpress
    Member
    Posted 3 years ago #

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

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

    I don't know what you mean by 'for links'

  6. iluvwrdpress
    Member
    Posted 3 years ago #

    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?

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

  8. iluvwrdpress
    Member
    Posted 3 years ago #

    how can i get only mobile users to be redirected?

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

  10. iluvwrdpress
    Member
    Posted 3 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic