How to redirect links for mobile using htaccess?
-
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 .? https://m.mysite.com%{REQUEST_URI} [L,R=302]
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘How to redirect links for mobile using htaccess?’ is closed to new replies.