4

I'm going to be starting on a mobile version of my website soon, already have space set up for it as follows:

http://mobile.example.com/

I found a solution here: Best way to redirect mobile devices

^ This seems to work just fine except when I try this from my iphone4 it will only redirect if I enter the www in the url.. if I leave it out I just get my regular site.

So I need the mobile redirect to work if the user enters www.example.com OR example.com

I made some modifications to the code and got it almost working. Now it works for both versions of the url but I have to refresh once to get the mobile site. Obviously I want the redirect to work when they first hit the site. I'm sure I'm missing something here, let me know what you think. I have aplus.net as my host btw.

RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com) [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ http://mobile.example.com/$1 [L,R=302]

Reponse to Jon Lin...

Yes there are a few other rules in my .htaccess

Defining different index page types:

DirectoryIndex index.html index.htm INDEX.HTML INDEX.HTM Index.html Index.htm default.htm Default.htm index.shtml index.cgi index.php index.php3 index.jsp index.phtml

Removing .php extension from urls

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Then this here which was put in automatically from the cp:

# [ CP Global Redirection - Start ]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_NAME} !^www\..*$
RewriteRule ^(.*)$ http://www.%{SERVER_NAME}/$1 [R]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{SERVER_NAME} !^www\..*$
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}/$1 [R]
</IfModule>
# [ CP Global Redirection - End ]
Community
  • 1
  • 1
turbo255
  • 41
  • 3
  • Are there any other rewrite rules or redirects setup in your server/vhost config or htaccess? – Jon Lin Jul 09 '12 at 19:54
  • Apparently I'm not familiar with responding to comments on here. I tried to paste in the other rules from my .htaccess but looks like it was too many characters? – turbo255 Jul 09 '12 at 22:16
  • Edit your post and add them to the bottom – Jon Lin Jul 09 '12 at 22:19
  • I suggest you use Javascript based redirection which is more robust than user agent guessing: https://github.com/miohtama/detectmobile.js – Mikko Ohtamaa Jul 11 '12 at 21:21

1 Answers1

0

Below is the Mobile redirect code we use on our Aplus.net platform for our mobile application. Should be able to use this to redirect to your own custom site.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:X-WAP-PROFILE} !^$ [OR]
RewriteCond %{HTTP:PROFILE} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(Alcatel|Asus|Android|BlackBerry|Ericsson|Fly|Huawei|i-mate|iPAQ|iPhone|iPod|LG-|LGE-|MDS_|MOT-|Nokia|Palm|Panasonic|Pantech|Philips|Sagem|Samsung|Sharp|SIE-|Symbian|Vodafone|Voxtel|WebOS|Windows\s+CE|ZTE-|Zune).*$ [NC,OR]
RewriteCond %{HTTP_ACCEPT} application/vnd.wap.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_ACCEPT} text/vnd.wap.wml [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_NAME} ^www\.(.*)$
RewriteRule ^(.*) http://mobile.%1/ [L]
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:X-WAP-PROFILE} !^$ [OR]
RewriteCond %{HTTP:PROFILE} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^.*(Alcatel|Asus|Android|BlackBerry|Ericsson|Fly|Huawei|i-mate|iPAQ|iPhone|iPod|LG-|LGE-|MDS_|MOT-|Nokia|Palm|Panasonic|Pantech|Philips|Sagem|Samsung|Sharp|SIE-|Symbian|Vodafone|Voxtel|WebOS|Windows\s+CE|ZTE-|Zune).*$ [NC,OR]
RewriteCond %{HTTP_ACCEPT} application/vnd.wap.xhtml\+xml [NC,OR]
RewriteCond %{HTTP_ACCEPT} text/vnd.wap.wml [NC]
RewriteRule ^(.*) http://mobile.%{HTTP_HOST}/ [L]