How to automatically redirect to a mobile site

How can I do automatic redirects on my website on my mobile website when visiting from a mobile browser. I would prefer not to create a database.

+3
source share
4 answers

Without platform information, it is difficult to make a specific recommendation. The general advice is to map some line in the User-Agent that tells you the mobile browser (you may need several templates) and generate a redirect .

EDIT: A Google search turns this very complete page into User-Agent strings for mobile devices. Suffice it to say that you probably want to limit your list of templates to devices specifically designed for targeting, and let the rest return to your main site.

+2
source

you must check the user agent header through regular expression to see if it is a mobile device or not.

look here http://detectmobilebrowser.com/

+2
source

, kanaacademy python:

def user_agent(self):
    return str(self.request.headers['User-Agent'])

def is_mobile_capable(self):
    user_agent_lower = self.user_agent().lower()
    return user_agent_lower.find("ipod") > -1 or \
            user_agent_lower.find("ipad") > -1 or \
            user_agent_lower.find("iphone") > -1 or \
            user_agent_lower.find("webos") > -1 or \
            user_agent_lower.find("android") > -1

https://khanacademy.kilnhg.com/Repo/Website/Group/stable/File/request_handler.py#115

In principle, a browser is mobile if the User-Agent request header with a bottom window contains the string ipod, ipad, iphone, webos or android. Once you know that the requestor is mobile, you can redirect to the mobile URL or configure your output for mobile devices.

0
source

This one worked for me. Take a picture:

<script type="text/javascript">
  <!--
  if (screen.width <= 699) {
  document.location = "mobile.html";
  }
  //-->
</script>

Place it just below the body tag.

0
source

Source: https://habr.com/ru/post/1742642/


All Articles