diff --git a/public/_redirects b/public/_redirects index f9b145c..c5371c9 100644 --- a/public/_redirects +++ b/public/_redirects @@ -1,17 +1,3 @@ # redirects for pretty URLs - generated automatically +# Using rewrites to preserve original URL -/2024-01-06-why-does-every-guest-house-room-have-a-tv-screen /blog/2024-01-06-why-does-every-guest-house-room-have-a-tv-screen 200 -/2025-06-21-crowdsourcing-exam-problems /blog/2025-06-21-crowdsourcing-exam-problems 200 -/2025-07-05-hypothetical-grading-policy /blog/2025-07-05-hypothetical-grading-policy 200 -/2025-07-09-math-human-flourishing-struggle /blog/2025-07-09-math-human-flourishing-struggle 200 -/2025-07-17-teacher-training-initiative /blog/2025-07-17-teacher-training-initiative 200 -/2025-07-21-capacities-pkm /blog/2025-07-21-capacities-pkm 200 -/2025-08-07-nptel-underrated /blog/2025-08-07-nptel-underrated 200 -/2025-08-16-interactive-fiction-textbooks /blog/2025-08-16-interactive-fiction-textbooks 200 -/2025-08-27-vibecoding-manim-slides /blog/2025-08-27-vibecoding-manim-slides 200 -/2025-08-29-grok-a-book-new-editions /blog/2025-08-29-grok-a-book-new-editions 200 -/2025-09-05-pocket-shutdown /blog/2025-09-05-pocket-shutdown 200 -/2025-09-23-ten-years-iitgn /blog/2025-09-23-ten-years-iitgn 200 -/2025-11-17-iisc-time-tracking /blog/2025-11-17-iisc-time-tracking 200 -/2026-02-20-value-of-things-comment /blog/2026-02-20-value-of-things-comment 200 -/2026-02-25-mattermost-for-openclaw /blog/2026-02-25-mattermost-for-openclaw 200 diff --git a/src/components/Head.astro b/src/components/Head.astro index 285ce23..e4e7741 100644 --- a/src/components/Head.astro +++ b/src/components/Head.astro @@ -21,10 +21,12 @@ interface Props { const canonicalURL = new URL(Astro.url.pathname, Astro.site); -// For pretty URLs (e.g., /slug), use the pretty URL as canonical -const requestedPath = Astro.request.url.replace(Astro.site?.href || '', '/').replace(/\/$/, ''); -const isPrettyUrl = requestedPath.startsWith('/20') || /^\/\d{4}-/.test(requestedPath); -const finalCanonicalPath = isPrettyUrl ? requestedPath : Astro.url.pathname; +// For blog posts with date-slug format, show pretty URL in og:url/canonical +// This handles both direct /slug access and proxy /blog/slug access +const pathParts = Astro.url.pathname.split('/').filter(Boolean); +const blogPostMatch = pathParts[0] === 'blog' ? pathParts[1] : pathParts[0]; +const isBlogPost = blogPostMatch?.match(/^\d{4}-\d{2}-\d{2}-/); +const finalCanonicalPath = isBlogPost ? `/${blogPostMatch}` : Astro.url.pathname; const finalCanonicalURL = new URL(finalCanonicalPath, Astro.site); const { title, description, image = "/astro-micro.jpg" } = Astro.props; diff --git a/src/pages/[...id].astro b/src/pages/[...id].astro new file mode 100644 index 0000000..8c9630d --- /dev/null +++ b/src/pages/[...id].astro @@ -0,0 +1,23 @@ +--- +import { getCollection } from 'astro:content'; + +export async function getStaticPaths() { + const posts = await getCollection('blog'); + + // Generate paths for both /blog/slug and /slug + const paths = []; + + for (const post of posts) { + // /blog/slug - the original path + paths.push({ + params: { id: post.id }, + props: { slug: post.id }, + }); + } + + return paths; +} + +const { slug } = Astro.props; +return Astro.redirect(`/blog/${slug}`, 301); +---