Google Indexing & Re-directs - Issue!

Soldato
Joined
18 Oct 2002
Posts
4,020
Location
Wellington, NZ
Hi,

I've currently got a basic wordpress website that I'm having Indexing trouble with. Due to an API i'm using on it I have a desktop version but also a mobile version - the responsive desktop version doesn't work well at all on mobile devices which is why I set up two different versions.. This is probably my first mistake. The mobile page is just a single page though.

I've set up some php that redirects any of the desktop pages to the mobile page if it detects a mobile device. If you're viewing from a desktop, it will remain on the desktop version - this is what it's doing below.

Google indexing doesn't like this at all and has only indexed the main landing page. The mobile page and all the other sub pages aren't indexed due to the following;

Duplicate, Google chose different canonical than user - on the mobile page
Page with redirect - on all my sub pages

Does anyone have a smart solution or any ideas to handle something like this?

Cheers

Code:
function wpcb_redirect_to_mobile() {
    if ( wp_is_mobile() && (is_page( 'website' ) || is_page() ) ) {
        if ( ! is_page( 'mobile' ) ) { // Avoid redirect loop
            wp_redirect( 'https://www.website.com/mobile/' );
            exit();
        }
    } elseif ( wp_is_mobile() && get_post_ancestors( get_the_ID() )[0] === get_page_by_path( 'website' )->ID ) {
        global $post;
        $ancestors = get_post_ancestors( $post );
        $redirect_url = 'https://www.website.com/mobile/';

        foreach ( $ancestors as $ancestor ) {
            $redirect_url .= get_post_field( 'post_name', $ancestor ) . '/';
        }

        $redirect_url .= $post->post_name . '/';

        if ( ! is_page( 'mobile' ) ) { // Avoid redirect loop
            wp_redirect( $redirect_url );
            exit();
        }
    }
}
add_action( 'template_redirect', 'wpcb_redirect_to_mobile' );
 
Soldato
OP
Joined
18 Oct 2002
Posts
4,020
Location
Wellington, NZ
sorted - WP allows you to dynamically enable or disable elements based on browser, device etc. I just added both elements on there with the different conditions per device. So much easier. Cheers.
 
Last edited:
Back
Top Bottom