1

I am using codeigniter framework for my site and I have done two site one for accessing through mobile and another for accessing through pcs. both are in same domain . My site url is like this http://mydomain.comAnd my mobile is http://mydomain.com/celphone. I need to write htaccess rule which redirect to site url http://mydomain.com/celphone when i am accessing site from mobile and i need to rewrite to http://mydomain.com/ when i am accessing the site from pc.

How can i write rewrite rule in such case ,please help any help will be appreciated.

Soumya Mohanan
  • 316
  • 4
  • 15

3 Answers3

2

As scessor, said, it cannot be done with .htaccess. You need to analyse the browser's user agent for this.

Check out this mobile detection in php.

Usage is as easy as this

if ($detect->isMobile()) {
    header("location: mobile.yourdomain.com");
    exit;
}
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
  • This answer is plain wrong, detecting mobile browsers in a .htaccess is absolutely possible. It will probably also be faster, as the request will be redirected before it even reaches the PHP engine. See scessor's answer for a link to a working .htaccess solution. – Bendik Oct 03 '12 at 08:49
1

The .htaccess has no information about the device, so it can't recognize the difference. You have to do this with your source code. (see my update below)

E.g. in javascript analyze the browsers userAgent and redirect by setting window.location:

<script type="text/javascript">// <![CDATA[  
    var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
    if (mobile) {  
        window.location = "http://www.yoursite.com/mobile.html";  
    }  
// ]]></script>

=== UPDATE ===

Read the anwers of this question to see how you can redirect in the .htaccess.

Community
  • 1
  • 1
scessor
  • 15,995
  • 4
  • 43
  • 54
0

Try doing it with PHP or any other server side language, you can look for the user agent and determine where to redirect the user. You might also find a ready-made class to do the checking for you. I've always used this and worked like a charm http://detectmobilebrowsers.mobi/

slash197
  • 9,028
  • 6
  • 41
  • 70