Statik sitelerde IndexNow gönderimini otomatikleştirmek

IndexNow, bir sayfa eklendiğinde ya da değiştiğinde arama motorlarına "gel, buna bak" demenin en kısa yolu. Bing, Yandex, Seznam ve birkaç motor daha destekliyor. Google desteklemiyor, ama bu diğerlerinde işe yaramadığı anlamına gelmiyor.

Sitemi her güncellediğimde URL'leri elle göndermek istemedim. Bu yüzden sitemap'i okuyup son değişen URL'leri gönderen küçük bir betik yazdım.

Anahtar dosyası

Önce bir anahtar oluşturup sitenin köküne aynı isimli bir .txt dosyası olarak koyuyorsun. Motorlar, gönderimin gerçekten site sahibinden geldiğini bu dosyayla doğruluyor.

KEY=$(openssl rand -hex 16)
echo "$KEY" > "public/$KEY.txt"
echo "Anahtar: $KEY"

Sitemap'ten URL toplamak

Her şeyi göndermek yerine sadece son 7 günde değişenleri alıyorum. lastmod değerlerin doğruysa bu yeterli.

indexnow.php
<?php
declare(strict_types=1);

const HOST = 'johnflaxman.com';
const KEY  = 'buraya-anahtar';

$xml  = simplexml_load_file('https://' . HOST . '/sitemap.xml');
$since = strtotime('-7 days');
$urls = [];

foreach ($xml->url as $u) {
    if (isset($u->lastmod) && strtotime((string)$u->lastmod) >= $since) {
        $urls[] = (string)$u->loc;
    }
}

if (!$urls) {
    exit("Gönderilecek URL yok.\n");
}

$payload = json_encode([
    'host'        => HOST,
    'key'         => KEY,
    'keyLocation' => 'https://' . HOST . '/' . KEY . '.txt',
    'urlList'     => array_slice($urls, 0, 10000),
]);

$ch = curl_init('https://api.indexnow.org/indexnow');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => ['Content-Type: application/json; charset=utf-8'],
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
echo count($urls) . ' URL gönderildi, HTTP ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . "\n";

200 ya da 202 dönüyorsa iş tamam. 403 görürsen anahtar dosyası yanlış yerde ya da içeriği farklı demektir.

Zamanlamak

Sunucunda cron varsa günde bir kez çalıştırmak yeter:

0 6 * * * php /home/kullanici/indexnow.php >> /home/kullanici/indexnow.log 2>&1

Sonuç?

Sıralamalarım uçtu mu? Hayır. Ama yeni sayfalar Bing'de saatler içinde görünmeye başladı; eskiden bu birkaç gün sürüyordu. Beş dakikalık iş için fena değil.

Comments / questions

There's no comment section here. If you have a question or want to add something, message me on Telegram or send an email to [email protected]. Thanks!

Related pages