40 lines
1.8 KiB
PowerShell
40 lines
1.8 KiB
PowerShell
# W7: add <meta name="robots" content="noindex,follow"> to duplicate-canonical pages
|
|
$repo = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
|
|
$dupFile = Join-Path $repo 'seo-audit-2026-05-27\gsc\duplicate-canonical.txt'
|
|
if (-not (Test-Path $dupFile)) { Write-Host "Missing $dupFile"; exit 1 }
|
|
$urls = (Get-Content $dupFile) | Where-Object { $_ -match '^https://' }
|
|
Write-Host "Total duplicate-canonical URLs: $($urls.Count)"
|
|
|
|
function UrlToLocal([string]$url, [string]$repo) {
|
|
$p = $url -replace '^https?://www\.odoo-expertos\.com', ''
|
|
$p = $p.Split('#')[0].Split('?')[0]
|
|
if ($p -eq '' -or $p -eq '/') { return (Join-Path $repo 'index.html') }
|
|
if ($p -match '\.html?$') { return (Join-Path $repo ($p.TrimStart('/').Replace('/','\'))) }
|
|
$p = $p.TrimEnd('/')
|
|
return (Join-Path $repo ($p.TrimStart('/').Replace('/','\') + '\index.html'))
|
|
}
|
|
|
|
$opts = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
|
|
$noindexed = 0
|
|
$missing = 0
|
|
foreach ($u in $urls) {
|
|
$lf = UrlToLocal $u $repo
|
|
if (-not (Test-Path $lf)) { $missing++; continue }
|
|
$c = [System.IO.File]::ReadAllText($lf)
|
|
if (-not $c) { continue }
|
|
# Check if noindex already present
|
|
if ([regex]::IsMatch($c, 'name="robots"[^>]*noindex', $opts)) { continue }
|
|
# If a <meta name="robots" ...> exists, replace its content; else inject new meta
|
|
$existing = [regex]::Match($c, '<meta\s+name="robots"\s+content="[^"]*"\s*/?>', $opts)
|
|
if ($existing.Success) {
|
|
$repl = '<meta name="robots" content="noindex,follow">'
|
|
$c = $c.Substring(0, $existing.Index) + $repl + $c.Substring($existing.Index + $existing.Length)
|
|
} else {
|
|
# Inject before </head>
|
|
$c = [regex]::Replace($c, '</head>', '<meta name="robots" content="noindex,follow">' + "`r`n</head>", $opts)
|
|
}
|
|
[System.IO.File]::WriteAllText($lf, $c)
|
|
$noindexed++
|
|
}
|
|
Write-Host "Pages noindexed: $noindexed; missing local: $missing"
|