Create Multilingual SITEMAP with PHP
1136   2  

Create Multilingual SITEMAP with PHP

  02 Nov 2017
  Coding Category
Create Multilingual SITEMAP with PHP

  25% more discounted by using the Giliapps as code in HostGator

If your website has equivalent versions of languages, you can use Multilingual Sitemap. This Sitemap help Google serve the correct language or regional URL to searchers. Lets start to create it with PHP and then automate creation with Cron Jobs.

1. Create a file with name sitemap.php in root

2. Create a file with name sitemap.xml in root

3. Place these codes for STATIC pages in your website (I use for language for example, also config.php in root use to connect MySQL, for more info see this post). You should to repeat $sitemap .= '***'; for other static pages in your website:

<?php

    header("Content-Type: application/rss+xml; charset=UTF-8");

    include_once("config.php");

    $sitemap = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/" />

            <changefreq>daily</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/sv/</loc>

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/" />

            <changefreq>daily</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/ka/</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/" />

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/" />

            <changefreq>daily</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/fa/</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/" />

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/" />

            <changefreq>daily</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/sample-static.php</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/sample-static.php" />

            <changefreq>monthly</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/sv/sample-static.php</loc>

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/sample-static.php" />

            <changefreq>monthly</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/ka/sample-static.php</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/sample-static.php" />

            <changefreq>monthly</changefreq>

            <priority>0.8</priority>

        </url>';

    $sitemap .= '

        <url>

            <loc>https://www.yourdomain.com/fa/sample-static.php</loc>

            <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/sample-static.php" />

            <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/sample-static.php" />

            <changefreq>monthly</changefreq>

            <priority>0.8</priority>

        </url>';

4. Add these codes for DYNAMIC pages in your website, like Blog Posts:

    $query = "SELECT * FROM posts ORDER BY id DESC";

    if($result = mysqli_query($db, $query)) {

        while($row = mysqli_fetch_array($result)) {

            $sitemap .= '

                <url>

                    <loc>https://www.yourdomain.com/blog/' . $row['post_url'] . '</loc>

                    <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/blog/' . $row['post_url'] . '" />

                    <lastmod>' . gmdate('Y-m-d\TH:i:s+00:00', strtotime($row['post_date'])) . '</lastmod>

                    <changefreq>daily</changefreq>

                    <priority>0.8</priority>

                </url>';

            $sitemap .= '

                <url>

                    <loc>https://www.yourdomain.com/sv/blog/' . $row['post_url'] . '</loc>

                    <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/blog/' . $row['post_url'] . '" />

                    <lastmod>' . gmdate('Y-m-d\TH:i:s+00:00', strtotime($row['post_date'])) . '</lastmod>

                    <changefreq>daily</changefreq>

                    <priority>0.8</priority>

                </url>';

            $sitemap .= '

                <url>

                    <loc>https://www.yourdomain.com/ka/blog/' . $row['post_url'] . '</loc>

                    <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="fa" href="https://www.yourdomain.com/fa/blog/' . $row['post_url'] . '" />

                    <lastmod>' . gmdate('Y-m-d\TH:i:s+00:00', strtotime($row['post_date'])) . '</lastmod>

                    <changefreq>daily</changefreq>

                    <priority>0.8</priority>

                </url>';

            $sitemap .= '

                <url>

                    <loc>https://www.yourdomain.com/fa/blog/' . $row['post_url'] . '</loc>

                    <xhtml:link rel="alternate" hreflang="sv" href="https://www.yourdomain.com/sv/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="ka" href="https://www.yourdomain.com/ka/blog/' . $row['post_url'] . '" />

                    <xhtml:link rel="alternate" hreflang="en" href="https://www.yourdomain.com/blog/' . $row['post_url'] . '" />

                    <lastmod>' . gmdate('Y-m-d\TH:i:s+00:00', strtotime($row['post_date'])) . '</lastmod>

                    <changefreq>daily</changefreq>

                    <priority>0.8</priority>

                </url>';

        }

    }

    $sitemap .= '</urlset>';

    file_put_contents("./sitemap.xml", $sitemap);

?>

5. You can create your Sitemap, manually by opening your sitemap.php in browser, or automate it with Cron Jobs. For example you can set generation it once a day and place this code in Cron Jobs:

/opt/php56/bin/php /home/yourdoma/public_html/sitemap.php

6. If your robots.txt has your sitemap, there is no need to Ping it to Search Engines, but if you want to force Ping, you can ping manually in Webmasters or use online services for it like XMLSITEMAPGENERATOR. This is a sample of robots.txt:

User-Agent: *

Disallow: /some-folder-to-prevent-indexing

Allow: /

Sitemap: https://www.yourdomain.com/sitemap.xml

7. Make me happy by insert a comment for ping sitemap to services with PHP to add them to current sitemap.php.

Author: Ismael Azaran

Do you like it? Share it with your friends.

Categories

Tags