redirect using htaccess all media in my site into mobile version

62 views Asked by At

How to redirect using htaccess when mobile user acces on my jpg,bmp,gif files on my site

example : i want to redirect mysite.com/media/upload/files.jpg|jpeg|bmp|gif|png into m.mysite.com/media/upload/files.jpg|jpeg|bmp|gif|png

Here is my htaccess

RewriteEngine On
RewriteRule \.(jpg|png|gif|jpeg|bmp)$ - [L]
RewriteCond %{HTTP_USER_AGENT} (mobile|android|blackberry|brew|cldc|docomo|htc|j2me|micromax|lg|midp|mot|motorola|netfront|nokia|obigo|openweb|opera.mini|palm|psp|samsung|sanyo|sch|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windows.ce) [NC]
RewriteRule ^(.*)$ m.mysite.com [R=302,L]

the problems is, when mobile user straight accessing http:// mysite.com/media/upload/files.jpg it still not redirect into m.mysite.com/media/upload/files.jpg

1

There are 1 answers

0
Jon Lin On

Assuming that you're checking against the correct user agents, try:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^m\.mysite\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} (mobile|android|blackberry|brew|cldc|docomo|htc|j2me|micromax|lg|midp|mot|motorola|netfront|nokia|obigo|openweb|opera.mini|palm|psp|samsung|sanyo|sch|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windows.ce) [NC]
RewriteCond $1 \.(jpg|png|gif|jpeg|bmp)$ [NC]
RewriteRule ^(.*)$ http://m.mysite.com/$1 [R=302,L]