Writing a PHP Google Sitemap generator without using fopen
(And yes I am fully aware the formatting of comments is rather nasty but it'll do)
The Google sitemap is a sitemap.xml file that you place in the document root of your website (which you inform Google of). It enables the popular search engine to index the pages in your site more accurately - rather than relying on the Googlebot to do all the hard work.
Automate the process
There are plugins for Wordpress that update the sitemap.xml file every time you publish (as there are for other content-managed solutions) but if you build your own site then you have to generate the file manually. Previously I've relied on giving the URL to a sitemap generator, saving it's output and uploading it to the server via FTP. I got bored of this so wrote my own generator that I could give to Google and forget about it. I'll explain how...
#1 - Set the header
Create a new file called sitemap.php. You need to set the header so that when you view the sitemap.php file it outputs as XML:
#2 - Open the connection to your database and do the query
However you store your information, it'll still be databased so the normal PHP connection/query code applies - this code is lifted directly off my site directly:
#3 - Start the XML document
Now that we've got all the current/published articles selected into a dataset we need to write the start of the XML document:
#4 - Work out the URL-Product
This is the thing that's most likely to differ depending on how you've developed your site. At Seopher.com I use URL-rewriting to associate a clean-URL with the ID of the article. Therefore what is realistically "http://www.seopher.com/viewarticle.php?id=5" becomes "http://www.seopher.com/articles/an_article_title". So the code I use to produce the URL-product is:
Whereas if you're using Wordpress-esq conventions (ie. www.seopher.com/article.php?id=5) then the code would look more like:
So all you need to do is work out how to make a real URL out of your databased content and then move on to step 5.
#5 - Output a list of your databased URLs
Now that you've worked out how to create a working URL-product of your content, it's time to output that into an XML schema that Google can make sense of.
The above code loops through the resultset and outputs the content into the XML schema that Google expects. The "lastmod" field is populated by re-formatting the timestamp you *should* have against when your article was posted. The "loc" assett is populated using the URL-product we made earlier.
#6 - Close everything down
It's just a case of closing the connection and ending the XML document.
And that's it as far as outputting everything in XML format. So you can upload the sitemap.php file to your server, load it into the browser and you should (hopefully) see a mess of all your content. View the source of the page and you should see something like:
Obviously that's my sitemap.php output (which has more than two items in it I might add) but you should see something to that effect. If you don't you'll need to troubleshoot what's causing problems. However, once you've got sitemap.php outputting something like you can see in the area above - then you can move on to step #7.
#7 - Modify the .htaccess file so sitemap.php becomes sitemap.xml
This is a crucial step because Google needs to see a SITEMAP.XML file and all you've got is SITEMAP.PHP. What you need to do is either edit or create a .htaccess file with the following logic in it:
What this does is it turns on the rewrite rule (allowing you to modify how URLs are handled, essentially) and adds the logic that allows a file.php to be intepreted as file.xml.
This now means that if you put sitemap.xml into your browser you'll be viewing the output from sitemap.php and that's crucial because now when Google looks for sitemap.xml it's viewing live data from your PHP script. This means that your sitemap.xml file will never be inaccurate.
Conclusion of what you should have
A sitemap.php file on your server that you can access by entering "www.yourwebsite.com/sitemap.php" or "www.yourwebsite.com/sitemap.xml" into your browser. This means that you now have a constantly up-to-date sitemap.xml file because you're not having to get it generated by a third party and upload it to your server.
How to improve it
My sitemap generator doesn't index my static pages (or even the homepage) because the homepage is already indexed sufficiently and I consider the other pages (contact, about etc) to be of no use to search engines. They're easily accessible from the navigation too so Googlebot shouldn't have any problems indexing them anyway.
Why it's good
Most hosts disable the use of the PHP function fopen which you need to write a physical sitemap.xml file, so this method bipasses the physical creation and instead references the PHP file as an XML document.
Hope this was useful.
Enjoy this article? Why not subscribe to the full RSS feed?





Showing most recent 20 of 50 comments [View all comments]
Sport is the best way to keep you healthy, fit and fine. Nike Kobe IV is one that gathers a lot of people, not only as fans that sit and watch others playing but also many who jump in the field and play.There is a long list of Lebron James Shoes helping out an athlete to do their best with light in weight footwear without abrasive feet.Names like Air Max 360, Nike, Reebok, Puma, and Dr. J are known in this regard, throughout the world.
Nowadays we can see that an athlete's clothing holds an important standing to give out the best performance. In sports outfits, Nike Air Max holds the most important place, Nike Air Shoes, as foot is the base of any performance whether sports or any other physical activity Shox Dream Shoes. In basketball, a lot of companies have gained fame with their awesome products.
That's awesome! I can't wait to get into it.
just like this. I really love it. Thanks!
coach purses
coach bags
cheap coach handbag
coach designer handbags
coach handbag
coach replica handbags
coach wallets , replica coach wallets
ed hardy handbag
ed hardy purse
ed hardy wallets
juicy handbag
cheap juicy handbags
juicy wallets
tous handbag
cheap tous handbag
tous wallets
chanel handbag
cheap chanel handbag
chanel wallet
louis vuitton handbag
cheap louis vuitton
louis vuitton wallet
gucci handbag
discount gucci handbags
gucci wallet
d&g handbag
d&g handbag bag
cheap d&g wallets
prada handbag
prada wallets
cheap prada handbags
christian audigier wallets
christian audigier handbag
christian audigier bag
christian audigier bags
fendi handbag
fendi bag
fendi wallet
miu miu handbag
cheap miu miu handbag
cheap miu miu wallets
dooney&bourke handbag
cheap dooney&bourke handbag
cheap dooney&bourke wallets
burberry handbag
cheap burberry handbags
burberry wallets
brand sunglasses
armani sunglasses
burberry sunglasses
cartier sunglasses
chanel sunglasses
Christian Dior Sunglasses
Coach Sunglasses
D&G Sunglasses
ED Hardy Sunglasses
Fendi Sunglasses
Ferragamo Sunglasses
Gucci Sunglasses
Louis Vuitton Sunglasses
Nike Sunglasses
Okey Sunglasses
Police Sunglasses
Prada Sunglasses
RayBan sunglasses
Versace Sunglasses
Wholesale coach handbags
cheap coach handbags
cheap coach handbag
coach designer handbags
replica coach handbags
new at coach
coach leather handbag
cheap coach purses
coach replica handbags
cheap coach bag
coach outlet
authentic handbags
cheap coach
coach discount
buy cheap handbags
discount cheap handbags
fashion coach handbag
authentic coach handbags
leather coach handbags
wholesale coach purses
coach online
buy coach bags
cheap coach purse
coach wholesale
discount coach bag
discount coach handbags
discount coach handbag
discounted coach handbags
discounted coach handbag
discount coach purses
replica coach handbags
discount designer handbags
designer handbags
discount coach
coach handbags on sale
cheap coach purses
coach replica bags
replica coach
clearance coach handbags
fake coach handbags
discount coach wallets
cheap coach handbag
authentic coach handbags
coach designer handbags
coach bags on sale
fake coach bags
cheap handbags wholesale
discount designer purses
cheap coach bag
authentic handbags
replica coach purses
wholesale designer handbags
buy coach handbags
coach handbags sale
coach hand bags
coach bags sale
coach bags online
fendi handbags
fashion handbags
cheap replica coach handbags
discount coach bag
wholesale coach handbag
discounted handbags
discount designer handbag
authentic coach handbag
fake coach
coach diaper bags
handbags on sale
coach wholesale
inspired handbags
fake coach purses
coach purses on sale
authentic designer handbags
coach sunglasses
replica designer handbag
ralph lauren handbags
coach luggage bags
coach handbags 2008
coach handbags online
new coach handbags
coach handbags store
authentic coach bag
spring coach bags
coachhandbags
classic coach handbag
buy coach bags
coach onsale
coach ergo handbags
coach hand bag
1sale coach bag
ladies coach handbags
coach baby bag
discount coach outlet
red coach purse
coach leather handbags
coach leather handbag
coach sling bags
coach shoulder bags
coach leather bags
hand bags
cheap coach outlet
buy cheap coach bag
buy cheap coach purses
buy cheap coach bags
buy cheap coach handbags
coach purses zoe
coach handbag zoe
coach wholesale
cheap replica coach handbags
cheap replica coach bags
buy replica coach bags
buy replica coach handbags
buy replica coach purses
china coach purses
buy china coach purses
wholesale coach purses
buy designer coach purses
buy designer coach handbags
buy designer coach bags
coach of new york
i like coach
coach nice
buy nice coach handbags
coach com
coach usa
coach make of
buy fake coach handbags
buy fake coach purses
buy fake coach bags
coach made of
cheap of coach
coach gift
coach women's
coach of women's
fake coach handbag
fake coach purse
fake coach bag
fake coach bags
fake coach purses
wholesale designer coach handbags
real coach handbags
buy real coach handbags
usa coach handbags
Very happy I can comment here!
Best wishes for this year.
Fancy knowing that.I'm counting on you.
Your article is very useful!
you can click here to know more:
http://www.shoesiii.com; --UGG boots,timberland boots
http://www.mylacoste.com/ --NFL jerseys
http://www.ghdlinks.com --hair straightener flat
http://www.hibose.com/ --Bose In-Ear Headphones
Timberland Roll-Top Boots menwaterproof leather for comfort, durability and abrasion resistance; Direct-attach, seam-sealed, waterproof construction keep feet dry in any weather ; Padded collar for a comfortable fit around the ankle and help keep out debris ; When rolled down the leather lining is exposed ; Durable laces with Taslan? fibers and rustproof hardware for long-lasting wear ; Footbed and inside of shoe is completely lined with soft, breathable leather ; Non-marking, rubber lug outsole for traction and durability