: Generate a sitemap automatically, does it need to be XML? 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
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?
More posts by @Kimberly868
3 Comments
Sorted by latest first Latest Oldest Best
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: 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 = "<as[^>]*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>'
}
}
?>
You are asking two questions here.
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.
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:
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.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.