• Hi everyone,

    As my topic title, I need to generate on the fly for any page the tag

    <link rel=”alternate” media=”only screen and (max-width: 640px)” href=”www.example.com/MobileSite/correspondingPHPpage.html”>

    Because I have got a Desktop site (wordpress) and a mobile site so I need to put in relation all pages that match, in order to don’t let take from the crawler duplicate pages.

    How can I update the href based on the page I am viewing.

    I need to do this, because I have read it on Google documentation, here it is:
    https://developers.google.com/webmasters/smartphone-sites/details#separateurls

    I thought it was possible to do that throughout jQuery but I didn’t find the right place where is explained it.
    Could be made by php? How?

    Any suggestion or documentation is appreciated,

    thank you in advance.

Viewing 7 replies - 1 through 7 (of 7 total)
  • This can be done through jQuery, but a better way of doing this would be through php.

    You can perform browser detection in php and then based on that load the appropriate link attribute.

    For Eg.

    <?php
      if( desktop browser ){
          <link href="browser site">
      }else{
          <link href="mobile site">
      }
    ?>
    Thread Starter manfra82

    (@manfra82)

    thank you for your answer,

    sorry I didn’t get why should I check the browser type?

    I have got a javascript functions that check the useragent and redirect to the Mobile site and viceversa on the Mobile one, but I guess are missing some information in my first request.

    (Desktop Site made by WordPress)
    https://www.example.com

    Mobile site made simple html and jQuery mobile, it is a subfolder but than will be a new TLD “example.mobi” nothing connected with wordpres even though is the same site made only for mobile)
    https://www.example.com/Mobile/Index.html

    So I was thinking to solve the issue in this way what do you think?
    Could this work fine? But I need to write an “if” statement for each page that has got a correspondence with the Mobile Site.

    <head>
    <link rel=”alternate” media=”only screen and (max-width: 640px)” href=”<?php CheckAlternate(); ?>” >
    </head>

    <?php
    function CheckAlternate(){
    if strstr(curPageURL,”page2″)
    return “www.example.com/Mobile/MobilePage2.html”
    }

    function curPageURL() {
    $pageURL = ‘http’;
    if ($_SERVER[“HTTPS”] == “on”) {$pageURL .= “s”;}
    $pageURL .= “://”;
    if ($_SERVER[“SERVER_PORT”] != “80”) {
    $pageURL .= $_SERVER[“SERVER_NAME”].”:”.$_SERVER[“SERVER_PORT”].$_SERVER[“REQUEST_URI”];
    } else {
    $pageURL .= $_SERVER[“SERVER_NAME”].$_SERVER[“REQUEST_URI”];
    }
    return $pageURL;
    }
    ?>

    In this way the Client makes a request and the response contain already the correct tag <link rel=”alternate”, Could this be right?

    Thank you again

    The way you are talking about is quite tedious.

    I was preferring browser detection because a single code on all the pages will check whether the request is coming from a desktop or a mobile.

    Based on that it will add the suitable link attribute.

    The code that you are using for redirection is fine but for dynamic link attribute I would go for browser detection.

    Thread Starter manfra82

    (@manfra82)

    thank you, so I want to follow your suggestion could you give a snippet a link where I can see how to do that sorry to be tedious ?? I have a very little knowledge about php, my english doesn’t help either ??
    If you could I’ll appreciate, otherwise thank you anyway for your patient

    First of all download the file Mobile_Detect.php from the following link https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php

    Note – This library is used by many users around the world for mobile detection. Even wordpress uses it ??

    Add it to your project.

    After you add it, Include the following code snippet in your header.

    include("Mobile_Detect.php"); /* put the path where you have added the mobile detect file */
    $detect = new Mobile_Detect();
    if ($detect->isMobile() || $detect->isTablet()) {
        <link href="mobile/tablet">
    }else{
        <link href="desktop">
    }
    Thread Starter manfra82

    (@manfra82)

    Amazing, thank you very much indeed,
    I’ll try it straight away

    Fra

    Cheers ?? Have a good day.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to create a tag link "alternative" dynamically’ is closed to new replies.