Tutorial on Creating Dynamic Sitemap Using PHP

If you own a website then one very common problem that you might come across is that some of your webpages might not show up in the search results. In such a case, most of us believe that the webpage does not have any links to them on the website. But unless the search engines have banned that web page, there are very less chances of any particular webpage not having links to it on the website. And in such a scenario, you are required to provide a proper navigation structure for your users, but that might not be a feasible option, especially when your website is having lot of content

. sitemap image with PHPWeb pages of any website can be hidden due to any reason. Creating a sitemap is the most workable solution to reduce the amount of hidden web pages not found in the search results of your site. Hearing this most of you will be thinking of searching of some website sitemap generator that will generate a sitemap for your site. But that might not be the best way. No one else can know your website better than you, also it is you who has the access to all the files of your website. Therefore, it is you who can create the best website sitemap for your users and the search engines.
 Sitemaps are preferably written in XML format, but it is not necessary for you to write XML code for each and every web page of your website manually. A better way is to build a XML sitemap together with PHP.

&lt;?php<br />
header(&quot;Content-Type: application/xml; charset=utf-8&quot;);<br />
echo &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#39;.PHP_EOL;<br />
echo&#39;&lt;urlsetxmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd&quot;&gt;&#39; .PHP_EOL;<br />
&nbsp;<br />
function urlElement($url) {<br />
&nbsp; echo &#39;&lt;url&gt;&#39;.PHP_EOL;<br />
&nbsp; echo &#39;&lt;loc&gt;&#39;.$url.&#39;&lt;/loc&gt;&#39;. PHP_EOL;<br />
&nbsp; echo &#39;&lt;changefreq&gt;weekly&lt;/changefreq&gt;&#39;.PHP_EOL;<br />
&nbsp; echo &#39;&lt;/url&gt;&#39;.PHP_EOL;<br />
}<br />
&nbsp;<br />
urlElement(&#39;http://www.programming.com/services/default-text-generator/&#39;);<br />
urlElement(&#39;http://www.programming.com/tests/practice/&#39;);<br />
urlElement(&#39;http://www.programming.com/tests/practice/HTML/&#39;);<br />
echo &#39;&lt;/urlset&gt;&#39;;<br />


In the above given example, we instantly send a header to inform the browser that the type of the content that is entered is XML and that it should render the page as XML. Then, we will set up the version information and where the schema is located, but you can merely copy all of that. Now, the interesting part is our urlElement function that takes the url as an argument and creates a url element in our sitemap. In the above example only changefreq and loc are used as a child element of url but, if you want then you can also use lastmod and priority to your url elements. But both of the elements can turn out to be useless, as search engines determine these elements on their own.

In case you have a dynamic website having content stored in a database, then you can create a function that loops through each url to that content. For this all you need to do is to place your content’s url inside the urlElement call during each loop.

To create dynamic sitemaps feel free to modify and build XML sitemap with the PHP code written as above.
About Author:
Celin is web developer and a team member of Xicom – Custom PHP Development team provides a talented PHP Developers For Hirefor all PHP Development Frameworks.

Share your comment

%d bloggers like this: