I searched around a lot for this one but couldn’t come to a simple conclusion, how could i add dynamic urls (domain.com/user=james) to a site map easily and on the fly?! well I came up with my own solution and it doesn’t need frameworks. It goes as follows:
$date = date('c', time());//date and time formatted for sitemap
$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];//gets the page and variables that this script is on
$lines = file('sitemap.xml');
$last = sizeof($lines) - 1 ;
unset($lines[$last]);
$fp = fopen('sitemap.xml', 'w');
fwrite($fp, implode('', $lines));
fclose($fp);
//^ the above code deletes the last line from the sitemap.xml file
$myfile = fopen("sitemap.xml", "a") or die("Unable to open file!");
$txt = "
$url
$date
";
fwrite($myfile, "". $txt);
fclose($myfile);
A sitemap.xml generally look similar to
https://domain/contact/ 2017-12-07T12:58:48+11:00 https://domain.com/about/ 2017-12-07T12:58:48+11:00 https://domain.com/faq/ 2017-12-07T12:58:49+11:00
With the PHP script above, it deletes the last line from the file which is the closing it then adds in a new with the location and time the location was last edited. Finally it closes out with the .
Its simple and it works, just use the script on your page that generates dynamic url’s and you will get them added to your sitemap and indexed in Google.
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…