I am having trouble redirecting to /admin/seo after form submission. The script insists to stay at https://www.example.com/admin/seo/submitIndexNow. My form is a simple button as follows:
My controller takes my sitemap.xml and preps it for submission to indexnow. Here is my controller:
data['admin_css'] .= '';
$this->data['admin_js'] .= '';
return view('/Admin/SEO/seo_index', $this->data );
}
public function submitIndexNow()
{
error_reporting(-1);
ini_set('display_errors', '1');
// Path to your sitemap
$sitemapPath="sitemap.xml";
// Load the sitemap
$sitemap = simplexml_load_file($sitemapPath);
if ($sitemap === false) {
return redirect()->to('/admin/seo')
->with('warning', 'Failed to open sitemap.xml');
}
// Extract URLs from the sitemap
$urlList = [];
foreach ($sitemap->url as $url) {
$urlList[] = (string) $url->loc; // Get the content
}
// Your API key and key location
// openssl rand -hex 16
$apiKey = "[deletedkeyhere]";
$host = site_url();
$keyLocation = site_url($apiKey);
// Prepare the payload
$payload = [
"host" => $host,
"key" => $apiKey,
"keyLocation" => $keyLocation,
"urlList" => $urlList // List of URLs extracted from the sitemap
];
// Convert payload to JSON
$payloadJson = json_encode($payload, JSON_PRETTY_PRINT);
// Call submitIt() to send the payload
$this->submitIt($payloadJson);
}
public function submitIt($payloadJson)
{
// API endpoint for IndexNow
$endpoint = "https://api.indexnow.org/indexnow";
// Initialize cURL
$ch = curl_init($endpoint);
// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Get HTTP response code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Handle response
if ($httpCode === 200) {
return redirect()->to('/admin/seo')
->with('info', 'Success - sitemap.xml was submitted to IndexNow');
} else {
return redirect()->to('/admin/seo')
->with('warning', 'Failed to submit URLs. HTTP Code: ' . $httpCode)
->with('response', $response);
}
}
}
The submission fails which is fine. Curl has no errors, however, httpCode replies with an error (eg not 200) which is fine but I cannot get the script to redirect back to /admin/seo where I want to display the errors. I always end up at a blank screen at https://www.example.com/admin/seo/submitIndexNow. I know for a fact that the script handles the HTTP response correctly and get to the redirect portion of the script. In fact, it gets to the warning part. If I place something like echo 'here'.die();
just before the return direct, it works… I just can’t get the redirect to go back to /admin/seo. It insists on staying at https://www.example.com/admin/seo/submitIndexNow. Any help super appreciated.
These are my routes:
$routes->add('/admin/seo', 'Admin\SEO\SEO_Controller::SEO_index');
$routes->add('/admin/seo/submitIndexNow', 'Admin\SEO\SEO_Controller::submitIndexNow');