Implement multi-domain SEO improvements:
- Add metadata to login and login-verify pages to prevent indexing. - Update robots.txt to disallow additional sensitive paths. - Enhance sitemap generation to filter by city and include accurate last modified dates. - Refactor canonical URL generation to support multi-domain architecture, ensuring self-canonicalization for city domains. - Remove deprecated CanonicalHandler component and streamline canonical URL handling. - Introduce safe JSON-LD output to prevent XSS vulnerabilities. - Add payment layout with appropriate metadata to prevent indexing. - Conduct a comprehensive technical SEO audit and implement necessary fixes across the application.
This commit is contained in:
+39
-69
@@ -1,80 +1,50 @@
|
||||
import { headers } from "next/headers";
|
||||
import citiesData from "@/data/city.json";
|
||||
|
||||
const MAIN_DOMAIN = "https://nobat724.com";
|
||||
// استراتژی چند-دامنهای: هر دامنهی شهری یک property مستقل و first-class است.
|
||||
// هر صفحه self-canonical روی هاست جاری میگیرد؛ هرگز به دامنهی اصلی canonical نمیدهیم.
|
||||
|
||||
const DEFAULT_HOST = "nobat724.com";
|
||||
|
||||
export function normalizeHost(rawHost) {
|
||||
return (rawHost || DEFAULT_HOST).toLowerCase().replace(/^www\./, "");
|
||||
}
|
||||
|
||||
export function buildCanonicalPath(pathname) {
|
||||
let path = (pathname || "/").split("?")[0].split("#")[0];
|
||||
if (!path.startsWith("/")) path = `/${path}`;
|
||||
if (path.length > 1 && path.endsWith("/")) path = path.slice(0, -1);
|
||||
return path;
|
||||
}
|
||||
|
||||
export function buildCanonicalUrl(host, pathname) {
|
||||
const normalizedHost = normalizeHost(host);
|
||||
const hostname = normalizedHost.replace(/:\d+$/, "");
|
||||
const protocol =
|
||||
hostname.endsWith("localhost") || hostname === "127.0.0.1" ? "http" : "https";
|
||||
return `${protocol}://${normalizedHost}${buildCanonicalPath(pathname)}`;
|
||||
}
|
||||
|
||||
// origin هاست جاری (بدون trailing slash) — برای ساخت URLهای مطلق در JSON-LD و متادیتا
|
||||
export async function getRequestOrigin() {
|
||||
try {
|
||||
const headersList = await headers();
|
||||
return buildCanonicalUrl(headersList.get("host"), "/").replace(/\/$/, "");
|
||||
} catch {
|
||||
return "https://nobat724.com";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current domain should have a canonical tag
|
||||
* and returns the canonical URL if needed
|
||||
*/
|
||||
export async function getCanonicalUrl() {
|
||||
try {
|
||||
const headersList = await headers();
|
||||
const host = headersList.get("host") || "";
|
||||
const pathname = headersList.get("x-pathname") || "/";
|
||||
|
||||
// Normalize host (remove www and convert to lowercase)
|
||||
const normalizedHost = host.toLowerCase().replace(/^www\./, "");
|
||||
|
||||
// Check if current domain is the main domain
|
||||
if (normalizedHost === "nobat724.com") {
|
||||
return null; // No canonical needed for main domain
|
||||
}
|
||||
|
||||
// Check if this is a subdomain/different domain from our city data
|
||||
const subdomain = normalizedHost.split(".")[0];
|
||||
const matchedCity = citiesData.find((city) => {
|
||||
const cityDomain = city.domain.toLowerCase();
|
||||
return cityDomain.includes(subdomain) || cityDomain === normalizedHost;
|
||||
});
|
||||
|
||||
// If we found a matching city or any non-main domain, canonicalize to main domain
|
||||
if (matchedCity || normalizedHost !== "nobat724.com") {
|
||||
// Ensure pathname starts with /
|
||||
const cleanPathname = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
||||
return `${MAIN_DOMAIN}${cleanPathname}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
const host = headersList.get("host");
|
||||
const pathname = headersList.get("x-pathname");
|
||||
// مسیرهای خارج از matcher میدلور (panel/dashboard/login) این هدر را ندارند —
|
||||
// آنها noindex هستند و نباید canonical جعلی به ریشه بگیرند.
|
||||
if (!pathname) return null;
|
||||
return buildCanonicalUrl(host, pathname);
|
||||
} catch (error) {
|
||||
console.error("Error generating canonical URL:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Client-side version for dynamic routes or client components
|
||||
*/
|
||||
export function getCanonicalUrlClient() {
|
||||
if (typeof window === "undefined") return null;
|
||||
|
||||
try {
|
||||
const host = window.location.host;
|
||||
const pathname = window.location.pathname;
|
||||
|
||||
// Normalize host (remove www and convert to lowercase)
|
||||
const normalizedHost = host.toLowerCase().replace(/^www\./, "");
|
||||
|
||||
// Check if current domain is the main domain
|
||||
if (normalizedHost === "nobat724.com") {
|
||||
return null; // No canonical needed for main domain
|
||||
}
|
||||
|
||||
// Check if this is a subdomain/different domain from our city data
|
||||
const subdomain = normalizedHost.split(".")[0];
|
||||
const matchedCity = citiesData.find((city) => {
|
||||
const cityDomain = city.domain.toLowerCase();
|
||||
return cityDomain.includes(subdomain) || cityDomain === normalizedHost;
|
||||
});
|
||||
|
||||
// If we found a matching city or any non-main domain, canonicalize to main domain
|
||||
if (matchedCity || normalizedHost !== "nobat724.com") {
|
||||
return `${MAIN_DOMAIN}${pathname}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Error generating client canonical URL:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user