0

I have successfully redirected my page to m.example.com from example.com when i open in mobile environment with this code in functions.php

add_action('template_redirect', 'redirecting_to_mobile_site');
function redirecting_to_mobile_site(){
$requri = $_SERVER['REQUEST_URI'];

 if( wp_is_mobile() )
 {
  $redcont = "http://m.example.com".$requri;
  wp_redirect( $redcont, 301 ); exit;
 }
}

I also created a sub-domain through WP sub-domain plugin.

But when I click any link from index page it says "This page has redirect loop"

Please advise how to enable sub-domain for internal pages.

Thank you.

0

1 Answer 1

0

You need to check that you aren't already on the mobile site:

function redirecting_to_mobile_site() {
    if ( $_SERVER['SERVER_NAME'] !== 'm.example.com' && wp_is_mobile() ) {
        wp_redirect( 'http://m.example.com' . $_SERVER['REQUEST_URI'], 301 );
        exit;
    }
}

add_action( 'template_redirect', 'redirecting_to_mobile_site' );

You'll need to clear your browser cache to properly test the fix.

8
  • i have used this code and desktop and mobile both version fail to load... please please provide other solutions
    – Racheal
    Aug 28, 2014 at 10:42
  • is this correct in your code? if ( $_SERVER['SERVER_NAME'] !== 'm.collegeswaggy.com' && wp_is_mobile() ) {
    – Racheal
    Aug 28, 2014 at 10:45
  • Use var_dump( $_SERVER['SERVER_NAME'] ); exit; and let me know what the result is. Aug 28, 2014 at 10:49
  • this comes on mobile and desktop browser string(17) "example.com"
    – Racheal
    Aug 28, 2014 at 10:54
  • Hmm, we'll need another way to detect if we're currently on the mobile site. Try print_r( $_SERVER ); exit; Aug 28, 2014 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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