How to create a sitemap - why do you need it? - what is it for?
Topics

Hi!
I'm Karsten and I want to help people learn German.
Buy me a coffee if you feel I did well.

How to create a sitemap - why do you need it? - what is it for?

A sitemap helps search engines to identify and index the content of your site easier. The simplest structure is this:


<?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> 

Don't bother too much about the cryptic head. Just copy it. XML is like html in that it requires an opening and a closing tag. The whole document is enclosed by <urlset > and </urlset> (corresponds to the body tag in html). The real important stuff is between <url> and </url>: links to your site's content. If your site consisted of only two urls it would look like this.


<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 <url>
  <loc>http://www.example.com/path/to/file/something.html</loc>
 </url>
 <url>
  <loc>http://www.example.com/path/to/file/more.html</loc>
 </url>
</urlset> 

So what you need is the cryptic head (copy and paste it), the urlset tag and lots of url tags. That's it. Name it 'sitemap.xml' and put it into your root folder (the folder where your index.html or index.php is located). Make sure to verify it in your search console with google's webmaster tools. If you have urls with GET-Parameters like 'http://www.primitivecode.com/index.php?topic=-default-Home-&code=3', you have to encode the '&'. Instead of '&' you have to write '&amp;'.

There are other characters that need to be encoded. But the ampersand is the most important character. This process of encoding the ampersand is called entity escaping.


http://www.your-cool-site.com/index.php?topic=cool-stuff&show=everything
http://www.your-cool-site.com/index.php?topic=more-cool-stuff&offer=even-better

These two urls have to be endoded like this:


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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 <url>
  <loc>http://www.your-cool-site.com/index.php?topic=cool-stuff&amp;show=everything</loc>
 </url>
 <url>
  <loc>http://www.your-cool-site.com/index.php?topic=more-cool-stuff&amp;offer=even-better</loc>
 </url>
</urlset> 

Then you also have the issue of url-encoding if you need special characters:

http://www.sitemaps.org/protocol.html says:
In addition, all URLs (including the URL of your Sitemap) must be URL-escaped and encoded for readability by the web server on which they are located.

The problem is that for example http://www.example.com/ümlat.php&q=name is not a valid url as it uses the German umlaut (ΓΌ).

Below is an example of a URL that uses a non-ASCII character (ü), as well as a character that requires entity escaping (&):

http://www.example.com/ümlat.php&q=name

Below is that same URL, UTF-8 encoded and URL escaped:

http://www.example.com/%C3%BCmlat.php&q=name

Below is that same URL, but also entity escaped:

http://www.example.com/%C3%BCmlat.php&amp;q=name

So what you do is:

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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 <url>
  <loc>http://www.example.com/%C3%BCmlat.php&amp;q=name</loc>
 </url>
</urlset> 

See a list of other valid tags.

Here's another example.


submit to reddit


Sneak peek of the server side code