In most cases, don’t redirect mobile visitors to a separate mobile site. Build one responsive site that adapts to the screen with CSS instead. Google indexes the mobile version of every site, and separate m. sites often have missing content, broken redirects and duplicate URLs. If you must keep a separate mobile site, redirect on the server, send each visitor to the matching page rather than the home page, add rel="alternate" and rel="canonical" links between the two versions, and let people switch back.
Originally published in February 2011 with a JavaScript user-agent list. Rewritten in December 2026 because the old approach now causes more problems than it solves.
Why the old approach breaks
The original script checked the browser’s user-agent against a list of keywords such as nokia, blackberry and mobile. Lists like that go out of date quickly. iPads now identify as desktop Safari, many phones were never on the list, and words like mot match unrelated browsers. Redirecting in JavaScript also loads the desktop page first, then sends the visitor elsewhere, which is slow on mobile connections.
The recommended fix: responsive design
One URL and one HTML document, styled differently per screen size:
<meta name="viewport" content="width=device-width, initial-scale=1">.layout { display: grid; grid-template-columns: 1fr; gap: 1rem; }
@media (min-width: 768px) {
.layout { grid-template-columns: 2fr 1fr; }
}Every visitor and every crawler sees the same content at the same address, so there’s nothing to redirect and nothing to keep in sync. Most WordPress themes and every current Shopify theme are responsive already.
If you must redirect
Sometimes a separate mobile site exists for a reason, such as a legacy app. Then:
- Redirect on the server, not in JavaScript, so the desktop page doesn’t load first.
- Send each URL to its mobile equivalent:
/products/blue-shirtshould go tom.example.com/products/blue-shirt, notm.example.com/. - Add
Vary: User-Agentto responses, so caches don’t serve the wrong version. - Link the two versions. On the desktop page:
<link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.example.com/page">. On the mobile page:<link rel="canonical" href="https://www.example.com/page">. - Offer a “View full site” link that sets a cookie, and skip the redirect when that cookie is present.
A simple .htaccess version:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteCond %{HTTP_COOKIE} !full_site=1
RewriteCond %{HTTP_USER_AGENT} Mobi|Android [NC]
RewriteRule ^ https://m.example.com%{REQUEST_URI} [R=302,L]
Header append Vary User-AgentThe Mobi keyword is what browser makers recommend checking for. A 302 keeps the desktop URL as the main one in search results.
Detecting mobile in JavaScript today
For small behaviour changes, test the screen or input type, not the device name:
const isSmallScreen = window.matchMedia('(max-width: 640px)').matches;
const isTouch = window.matchMedia('(pointer: coarse)').matches;Common questions
Does a separate mobile site hurt SEO?
It can. Google indexes the mobile version, so anything missing from your m. site is effectively missing from search. Wrong redirects and missing canonical tags also split rankings between two URLs.
What about AMP?
AMP pages are no longer required for Google’s Top Stories, and many sites have removed them. A fast responsive page does the same job.
How do I test my site on phones without a phone?
Use the device toolbar in Chrome or Firefox DevTools, then check a real phone before launch.