Can we have a sub-domain m.website.com for mobile users, those are automatically redirected to it?

And what's the purpose anyway?

  • That's the purpose, to serve optimized website for mobile and handheld devices.
    – Robert hue
    Jan 7 '15 at 3:28

You can have a subdomain, but you cannot detect mobile users reliably. And you shouldn’t.

How to use a subdomain with the same content

In your wp-config.php, look at $_SERVER['HTTP_HOST']. If it matches m.example.com, enforce the current domain as main domain with …

const DOMAIN_CURRENT_SITE = 'm.example.com';

… and filter the active theme:

add_filter( 'stylesheet', function( $stylesheet ) {

    if ( 'm.example.com' === DOMAIN_CURRENT_SITE )
        return 'mobile-theme';

    return $stylesheet;

Set up redirections for /wp-admin/ and wp-login.php, so authors cannot write on the mobile site.

Why you should not do that

Detecting mobile users is hard work. You cannot trust user agent strings, because they lie, are empty, or they contain malicious code. Never use wp_is_mobile(), this is embarrassingly naïve.

Being on a mobile device doesn’t mean that a small screen or low bandwidth is used.

The separate subdomains are a thing of the past. They were used when browsers could not handle media queries, and mobile were all small and slow. This isn’t true anymore, so please don’t start building a dinosaur.

See also: How to load content per AJAX depending on screen size.

  • How else to detect for mobile? For example, using mobile-based ads on a page where the only control over the ad is which tag gets called. The theme itself is responsive, but including, say, Flash-based ads for desktop and text-based ads for mobile?
    – Jon Mitten
    Jan 8 '15 at 19:16
  • 1
    @JonMitten I have added a link for that. I don’t see any reason to deliver Flash to anyone.
    – fuxia
    Jan 8 '15 at 19:27
  • While I agree in principle that Flash is unnecessary, content publishers don't necessarily have the flexibility or control over their ad feeds where they can pick and choose which ads get delivered.
    – Jon Mitten
    Jan 8 '15 at 19:56

You should rather considering just making your site perfectly responsive, subdomains for mobile websites are thing of past, but some big website do still rely on it.

Not because they want a mobile site, but because some of their main website features are yet not something that a mobile device should be left to handle.

We are now at a stage where applications(here : Web Apps) are platform independent, irrespective of the device they are run on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.