ozcankrks.devbankai
TR
All posts

Creating a Dynamic Sitemap with PHP

Build a sitemap so Google and other search engines crawl your pages at regular intervals.

A sitemap lets Google and other search engines crawl the links in your sitemap file at regular intervals, which helps you rank higher. Here I'll create a sitemap.xml with PHP. Since the sitemap will be dynamic, the data will come from our database, so we'll write it as sitemap.php and then use the .htaccess file to serve requests for sitemap.xml from sitemap.php.

First, download the sitemap.php file I linked below and edit the relevant parts of it (the database connection and the queries).

Then paste the following code into your .htaccess file:

RewriteEngine on

RewriteRule ^sitemap.xml$ sitemap.php [NC,L]

After pasting it, save all the files and upload them to your server. Try opening yoursite.com/sitemap.php. If the page that opens shows up as yoursite.com/sitemap.xml, check that the database part works correctly on that page. If you made it this far without problems, you're done.

You can download sample sitemap.php code below.

https://yadi.sk/d/R85DQG7ktsE1-g

Sitemap.php example

.htaccess example

Good luck!

Comments 0

No comments yet. Be the first.

Leave a comment

Never published.

© 2026 Özcan Karakuş · This site is built with Next.js and runs on my own server.