- robots.txt sperrt /api nicht mehr (Google lädt beim Rendern alle Inhalte und Bilder über die API); API-Antworten tragen dafür X-Robots-Tag: noindex - Suchmaschinen bekommen immer die deutsche Seite statt der englischen - page.php: 301 von Adressen der alten Website (/car-hifi/, /oldtimer/, /umbauten-einbauten/..., /ueber-uns/ ...) und von Adressen mit "/" am Ende; unbekannte Adressen liefern echten Status 404 - Modellbilder auf der Markenseite mit Alt-Text Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E4MG4y7kh1e72NTWq98Q8M
182 lines
6.6 KiB
PHP
182 lines
6.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Liefert die index.html der React-App aus (SPA-Fallback, siehe hifi/.htaccess) und setzt
|
|
* dabei die im Admin hinterlegten Bilder ein, die schon im HTML stehen muessen: das
|
|
* Link-Vorschaubild (og:image fuer WhatsApp, Facebook & Co. - deren Vorschau fuehrt kein
|
|
* JavaScript aus) und das Favicon. Ohne eigenes Bild oder wenn die Datenbank nicht
|
|
* erreichbar ist, geht die index.html unveraendert raus - die Seite bleibt also immer
|
|
* erreichbar.
|
|
*/
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use App\Support\SiteImages;
|
|
|
|
$html = @file_get_contents(__DIR__ . '/../../index.html');
|
|
if ($html === false) {
|
|
http_response_code(500);
|
|
exit('index.html fehlt - bitte "npm run build" ausfuehren.');
|
|
}
|
|
|
|
|
|
/** Eigene Datenbankverbindung, da Database::connection() bei Fehlern JSON ausgibt. Null ohne DB. */
|
|
function hifi_pdo(): ?PDO
|
|
{
|
|
static $pdo = false;
|
|
if ($pdo !== false) {
|
|
return $pdo;
|
|
}
|
|
$pdo = null;
|
|
try {
|
|
$config = require __DIR__ . '/../config/config.php';
|
|
$db = $config['db'];
|
|
if (!empty($db['host']) && !empty($db['name'])) {
|
|
$pdo = new PDO(
|
|
"mysql:host={$db['host']};dbname={$db['name']};charset={$db['charset']}",
|
|
$db['user'],
|
|
$db['pass'],
|
|
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 3]
|
|
);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$pdo = null;
|
|
}
|
|
return $pdo;
|
|
}
|
|
|
|
/** Gespeicherte Bilder lesen. */
|
|
function hifi_site_images(): array
|
|
{
|
|
try {
|
|
$pdo = hifi_pdo();
|
|
if (!$pdo) {
|
|
return [];
|
|
}
|
|
$raw = $pdo->query('SELECT site_images FROM app_settings WHERE id = 1')->fetchColumn();
|
|
return SiteImages::decode($raw);
|
|
} catch (\Throwable $e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/** Absolute URL auf diesem Server (Vorschau-Dienste brauchen eine volle Adresse). */
|
|
function hifi_absolute_url(string $path): string
|
|
{
|
|
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|
|
|| strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https';
|
|
$host = (string) ($_SERVER['HTTP_HOST'] ?? '');
|
|
if (!preg_match('/^[A-Za-z0-9.\-]+(:\d+)?$/', $host)) {
|
|
$config = require __DIR__ . '/../config/config.php';
|
|
return rtrim($config['app']['site_url'], '/') . $path;
|
|
}
|
|
return ($https ? 'https://' : 'http://') . $host . $path;
|
|
}
|
|
|
|
/**
|
|
* Adressen der alten Website (bis Okt. 2026), die Google noch im Index hat. Ein 301 sagt
|
|
* Google, wo die Seite jetzt steht, und gibt deren bisherige Bewertung an die neue weiter.
|
|
*/
|
|
const HIFI_OLD_PATHS = [
|
|
'#^/(car-hifi|wohnmobil[a-z\-]*|caravan[a-z\-]*|oldtimer|cnc[a-z\-]*|laser[a-z\-]*|3d-druck|alarmanlage[a-z\-]*|dash-?cams?)$#' => '/leistungen',
|
|
'#^/umbauten-einbauten(/.*)?$#' => '/galerie',
|
|
'#^/(home|ueber-uns|news|videos?)(/.*)?$#' => '/',
|
|
'#^/datenschutzerklaerung$#' => '/datenschutz',
|
|
];
|
|
|
|
/**
|
|
* Prueft, ob die React-App unter dieser Adresse eine Seite hat (Routen aus App.jsx). Bei
|
|
* Marken, Modellen und Galerie wird in der Datenbank nachgesehen; ist sie nicht erreichbar,
|
|
* gilt die Adresse als gueltig - lieber ein 200 zu viel als echte Seiten als 404 melden.
|
|
*/
|
|
function hifi_route_exists(string $path): bool
|
|
{
|
|
if (preg_match('#^/(admin|setup)(/|$)#', $path)) {
|
|
return true;
|
|
}
|
|
$static = ['/', '/fahrzeuge', '/galerie', '/leistungen', '/kontakt', '/downloads', '/impressum', '/datenschutz', '/agb'];
|
|
if (in_array($path, $static, true)) {
|
|
return true;
|
|
}
|
|
$queries = [
|
|
'#^/fahrzeuge/([^/]+)$#' => 'SELECT 1 FROM brands WHERE slug = ?',
|
|
'#^/fahrzeuge/([^/]+)/([^/]+)$#' => 'SELECT 1 FROM car_models m JOIN brands b ON b.id = m.brand_id WHERE b.slug = ? AND m.slug = ?',
|
|
'#^/galerie/([^/]+)$#' => 'SELECT 1 FROM gallery_brands WHERE slug = ?',
|
|
'#^/galerie/([^/]+)/([^/]+)$#' => 'SELECT 1 FROM gallery_projects p JOIN gallery_brands b ON b.id = p.gallery_brand_id WHERE b.slug = ? AND p.slug = ?',
|
|
];
|
|
foreach ($queries as $pattern => $sql) {
|
|
if (!preg_match($pattern, $path, $m)) {
|
|
continue;
|
|
}
|
|
$pdo = hifi_pdo();
|
|
if (!$pdo) {
|
|
return true;
|
|
}
|
|
try {
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute(array_map('rawurldecode', array_slice($m, 1)));
|
|
return (bool) $stmt->fetchColumn();
|
|
} catch (\Throwable $e) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
$path = (string) parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
|
$query = (string) parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_QUERY);
|
|
if ($path === '/index.html') {
|
|
$path = '/';
|
|
}
|
|
// Schraegstrich am Ende (alte WordPress-Adressen) entfernen, damit jede Seite nur eine Adresse hat.
|
|
$target = $path !== '/' ? rtrim($path, '/') : $path;
|
|
if ($target === '') {
|
|
$target = '/';
|
|
}
|
|
$exists = hifi_route_exists($target);
|
|
if (!$exists) {
|
|
foreach (HIFI_OLD_PATHS as $pattern => $newPath) {
|
|
if (preg_match($pattern, $target)) {
|
|
$target = $newPath;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ($target !== $path) {
|
|
header('Location: ' . $target . ($query !== '' ? '?' . $query : ''), true, 301);
|
|
exit;
|
|
}
|
|
// Unbekannte Adressen bekommen trotzdem die App (zeigt "Seite nicht gefunden"), aber mit
|
|
// echtem Status 404 - sonst haelt Google sie fuer eine leere Seite der Website.
|
|
if (!$exists) {
|
|
http_response_code(404);
|
|
}
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
header('Cache-Control: no-cache');
|
|
|
|
$images = hifi_site_images();
|
|
|
|
if (!empty($images['og_image'])) {
|
|
$url = htmlspecialchars(hifi_absolute_url($images['og_image']), ENT_QUOTES);
|
|
// Alle Verweise auf das eingebaute Vorschaubild (og:image, twitter:image, JSON-LD).
|
|
$html = preg_replace('#https?://[^"\s]*/og-image\.jpg#', $url, $html);
|
|
$size = @getimagesize(__DIR__ . '/../uploads/' . basename($images['og_image']));
|
|
if ($size) {
|
|
$html = preg_replace('#(<meta property="og:image:width" content=")\d+#', '${1}' . (int) $size[0], $html);
|
|
$html = preg_replace('#(<meta property="og:image:height" content=")\d+#', '${1}' . (int) $size[1], $html);
|
|
} else {
|
|
$html = preg_replace('#\s*<meta property="og:image:(width|height)" content="\d+" />#', '', $html);
|
|
}
|
|
}
|
|
|
|
if (!empty($images['favicon'])) {
|
|
$type = str_ends_with($images['favicon'], '.png') ? 'image/png' : (str_ends_with($images['favicon'], '.webp') ? 'image/webp' : 'image/jpeg');
|
|
$html = preg_replace(
|
|
'#<link rel="icon" type="image/png" href="[^"]*" />#',
|
|
'<link rel="icon" type="' . $type . '" href="' . htmlspecialchars($images['favicon'], ENT_QUOTES) . '" />',
|
|
$html
|
|
);
|
|
}
|
|
|
|
echo $html;
|