N
N
newbie32016-08-01 13:32:57
PHP
newbie3, 2016-08-01 13:32:57

How to create sitemap.xml in PHP?

i have sitemap.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      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">
<url>
  <loc>http://site.ru/</loc>
</url>
<url>
  <loc>http://site.ru/filmebi.php</loc>
</url>
<url>
  <loc>http://site.ru/musika.php</loc>
</url>

and in PHP code where I have this code:
switch($Page){
        case "index":
           include_once('resource/main.php');
        break;
        case "aboutus":
           include_once('resource/about.php');
        break;
        case "ads":
           include_once('resource/ads.php');
        break;
        case "news":
           include_once('resource/news.php');
        break;
        case "sitemap.xml":  //тут у меня проблема
           include_once('sitemap.xml' ); 
        break;
        case "robots.txt":
           include_once('robots.txt');
        break;
default:
           include_once('error/error404.php');

I do include_once('sitemap.xml' );but it says this error:
Parse error: syntax error, unexpected T_STRING in D:\OpenServer\domains\testi.dev\sitemap.xml on line 1

what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Koryukov, 2016-08-01
@MadridianFox

How do you pass the $Page parameter to the script?
Usually, when creating a php application with a single entry point, they use mod_rewrite, well, they cut off everything that comes after the domain, and assign the cut off to some get parameter.
You can do without mod_rewrite, but then the links will look like index.php?page=index
In the first case, you need to add a condition in .htaccess that the passed path is not a file or folder.
In the second case, nothing prevents the search bot, or anyone else from picking up this xml file from the root of the site, and you don’t need to make additional gestures to return it.
If you really really want to do it the way you do, then you need not to connect the xml file, but read its contents and display it on the page. Something like:

case "sitemap.xml":  
    echo get_file_contents('sitemap.xml'); 
break;

M
mr_molodoy, 2018-02-27
@mr_molodoy

I do not quite understand why you need to connect sitemap to php script?
sitemap.xml You should put it in your site's root directory (next to index.php) and it will be available at http://yoursite/sitemap.xml for search engines.
Nothing more needs to be done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question