|
In questo articolo spiegherò brevemente come creare una Sitemap per Google usando codice .asp. Nell'esempio ipotizzerò un sito che abbia delle pagine (.asp) relative ai propri prodotti del tipo: http://www.miosito.it/prodotti.asp?p=12345 e creerò una sitemap per Google che comprenda tutte le diverse pagine dei prodotti.
Nell'esempio prenderò in considerazione un sito su Hosting Windows (con pagine .asp) e un Database SQL Server 2000/2005. Il codice non è particolarmente difficile ma richiede un minimo di esperienza con codice .asp e query su database che non approfondirò. Se avete problemi lasciate commenti nell'articolo.
Credits: questo esempio prende spunto da alcuni esempi di Francesco Passantino.
Copiate il seguente codice, fate le opportune modifiche che ho segnato in grasetto (connessione al Database, URL di base delle proprie pagine .asp, Query sul Database) e salvatela nella root del vostro sito con estensione .asp (ad esempio sitemap_db.asp). Segnalatela nel Google Webmaster Tools ed il gioco è fatto! Codice: <%
'MODIFICARE CON LE IMPOSTAZIONI DEL PROPRIO DATABASE (ESEMPIO PER SQL SERVER): Dim mia_conn_STRING mia_conn_STRING = "Provider=sqloledb;Data Source=999.999.999.999;User Id=mio_user;Password=mia_password;"
MAXURLS_PER_SITEMAP = 50000
'MODIFICARE IN BASE AL PROPRIO SITO: baseurl="http://www.miosito.it/prodotti.asp?p="
'MODIFICARE LA QUERY IN BASE AL PROPRIO DATABASE: strsql = "SELECT * FROM PRODOTTI WHERE ........."
utcOffset=1
response.ContentType = "text/xml" response.write "<?xml version='1.0' encoding='UTF-8'?>" response.write "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84'>"
Set conn = Server.CreateObject("ADODB.Connection") conn.Open mia_conn_STRING Set rs = Server.CreateObject("ADODB.Recordset") rs.Open strsql, conn
Do while not rs.eof if URLS<MAXURLS_PER_SITEMAP then
'MODIFICARE IN BASE AI CAMPI DELLA PROPRIA TABELLA id_page=Server.HTMLEncode(rs("ID_PRODOTTO")) filelmdate=rs("DATA") 'IMPOSTA UNA PRIORITY RANDOM (GOOGLE DA' AVVISI SE IMPOSTATE LA STESSA PRIORITA' PER TUTTE LE URL) Randomize dim ma, mi, ris ris=(round(0.50*Rnd+0.50, 1)) priority=Replace(Cstr(ris), ",", ".") if not isdate(filelmdate) then filelmdate=now() filedate=iso8601date(filelmdate,utcOffset)
response.write "<url><loc>"&server.htmlencode(baseurl&id_page)&"</loc><lastmod>"&filedate&"</lastmod><priority>"&priority&"</priority></url>" URLS=URLS+1 Response.Flush rs.movenext end if Loop
response.write "</urlset>"
rs.Close Set rs = Nothing conn.Close Set conn = Nothing
Function iso8601date(dLocal,utcOffset) Dim d ' convert local time into UTC d = DateAdd("H",-1 * utcOffset,dLocal)
' compose the date iso8601date = Year(d) & "-" & Right("0" & Month(d),2) & "-" & Right("0" & Day(d),2) & "T" & _ Right("0" & Hour(d),2) & ":" & Right("0" & Minute(d),2) & ":" & Right("0" & Second(d),2) & "Z" End Function %>
|