3

I want to create an XML sitemap (for the sake of SEO), my first attempt to achieve this was by including the website main menu/navigation links using <?php include ("assets/includes/menu.inc"); ?>:

<li><a href="index.php">Home</a></li>
<li><a href="gallery.php">Gallery</a></li>
<li><a href="contact.php">Contact</a></li>

To the following XML sitemap file:

<?xml version="1.0" encoding="UTF-8"?>

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

        <url><loc>http://www.example.com/</loc></url> 

    </urlset>

I already asked a question here of "How to include PHP to XML" - apparently I can't do that, so my idea was thrown to the bin. I already Googled "How to create an automatic sitemap" but all what I have found was Sitemap Generators which I am trying to avoid.

The question is, is that even possible? to create a sitemap that will pull the links from the website main .inc file? Any ideas? Does it need to be XML? can it be php for example?

3 Answers 3

6

You are asking two questions here.

  1. Does a sitemap need to be XML?

    The simple answer is no, it doesn't have to be XML. It can be XML file, a text file or RSS/Atom feed (which is basically XML), HTML Sitemap

    HTML Sitemaps: These are used on your website to display the layout in layers on your website to any customer that would wish too (don't know why they would want too). These unfortunately are not suitable to submit to search engines.

    RSS Feeds: These are rather useful for a feed of comments or articles on a blog, which are constantly updating, which in a sense is like an automatic sitemap of new content/comments on a blog.

    XML Files: These are in simple terms a set of links in XML format with additional fields such as link and maybe a last modified field.

    Text Files: These used to be the preferred method of submitting to Yahoo, however a while back they started accepting XML feeds.

  2. Can you create a sitemap with a .php extension?

    Basically as long as your PHP file returns and XML header / mime-type and returns valid XML then you will be fine. Look at WordPress, their RSS feeds are created using PHP.

Use the PHP DOM function and set your headers:

header( "content-type: application/xml; charset=UTF-8" );
$doc = new DomDocument('1.0', 'UTF-8');

The documentation can be found here:

http://www.php.net/manual/en/class.domdocument.php

EDIT:

Looking at your previous post, you are trying to inject HTML into XML which will not work. If you can expand on why you wish to do this we may be able to offer some advice.

0
1

to create a sitemap that will pull the links from the website main .inc file?

The extension doesn't matter. So, you can have a php file like

<?php
header ("Content-Type:text/xml");
echo '<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        // code to extract and echo links from the file 
echo '   </urlset>';
?>

To extract the links from the html code, see this page.

The code should be modified to something like the one below.

<?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  $url = "assets/includes/menu.inc";
  $input = @file_get_contents($url) or die("Could not access file: $url");
  $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
  if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
      // $match[2] = link address
      // $match[3] = link text 
      echo '<url><loc>' . $match[2] . '</loc></url>'
    }
  }
?>
3
  • Couple of things, 1 don't use die, it's very unfriendly especially when trying to debug your code as it don't log your error and you don't want users seeing this error. Secondly, don't parse HTML using regular expressions, it's very bad practice, use the PHP DOMDocument parser Jan 31, 2014 at 9:07
  • I tried your code, there was an error echo '<url><loc>' . $match[2] . '</loc></url>' you were missing a ; at the end. I fixed that and tried it again and now It shows this: postimg.org/image/gh5d0k4sx any ideas of what is going on?
    – Leo
    Jan 31, 2014 at 14:15
  • I have tested it on my localhost and it was working. Here is the full code : gist.github.com/anonymous/4b02a53de8c0c5bbbaf0 . May be there is some problem with the $url on line 5. I am not much familiar with PHP. May be someone else can help you.
    – Ranjith
    Jan 31, 2014 at 17:34
0

You can add sitemaps with any extension to GWT, as long as it's formatted as an XML. My site has a .php sitemap where the PHP code generates an XML and it works perfectly.

1
  • Whether a file is seen as XML or not is determined by the mime-type ... set with the Content-Type HTTP response header eg. Content-Type: application/xml
    – MrWhite
    Feb 3, 2014 at 10:27

Your Answer

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

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