A
A
alexander_chn2017-09-09 20:37:47
Mobile development
alexander_chn, 2017-09-09 20:37:47

How to redirect to a mobile subdomain if the site is viewed from a phone?

Hello! The essence of the question is this:
There is a site on opencart. It lies on the domain let's say: domain.com. It is necessary to make it so that when a user enters the site from the phone, he is automatically redirected to m.domain.com Do
the following things:
Directly after the open head tag, write (connect a special library that recognizes what the user came from)

<script src="/catalog/view/javascript/device.min.js"></script>
<script>
  if ( device.mobile() ) {
    window.location.href = "http://m.domain.com/";
    //window.location = "http://m.domain.com/";
    //location.href = 'http://m.domain.com/';
   }
</script>

It did not help.
Then I connected this: https://opencartforum.com/topic/35925-mobilnaya-ve...
I had to screw in a special php class Mobile_Detect.php there, but it didn't help either.
I noticed a strange thing - when I go from the phone, I see the following in the address bar : domain.com/index.php?route=mobile/home
I can guess that this is a redirect to loading the mobile theme of the site, but not a redirect to a subdomain.
How to implement a redirect to a subdomain?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-09-11
@freelancer1114

Try .htaccess
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
RewriteCond %{HTTP_HOST} !^m\.domen\.com [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
RewriteRule ^/?(.*) m. domain.com/$1 [L,R,NE,QSA]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question