简体   繁体   中英

Mobile redirect with .htaccess for www.example.com OR example.com

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 ]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM