Add Netlify _redirects for pretty URLs with proper 301 redirects
This commit is contained in:
parent
9af0acc294
commit
f478bd6f0a
4 changed files with 46 additions and 17 deletions
|
|
@ -5,7 +5,8 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "astro dev",
|
"dev": "astro dev",
|
||||||
"start": "astro dev",
|
"start": "astro dev",
|
||||||
"build": "astro check && astro build",
|
"prebuild": "node scripts/generate-redirects.js",
|
||||||
|
"build": "npm run prebuild && astro check && astro build",
|
||||||
"preview": "astro preview",
|
"preview": "astro preview",
|
||||||
"astro": "astro"
|
"astro": "astro"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
17
public/_redirects
Normal file
17
public/_redirects
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
# redirects for pretty URLs - generated automatically
|
||||||
|
|
||||||
|
/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 301
|
||||||
|
/2025-06-21-crowdsourcing-exam-problems /blog/2025-06-21-crowdsourcing-exam-problems 301
|
||||||
|
/2025-07-05-hypothetical-grading-policy /blog/2025-07-05-hypothetical-grading-policy 301
|
||||||
|
/2025-07-09-math-human-flourishing-struggle /blog/2025-07-09-math-human-flourishing-struggle 301
|
||||||
|
/2025-07-17-teacher-training-initiative /blog/2025-07-17-teacher-training-initiative 301
|
||||||
|
/2025-07-21-capacities-pkm /blog/2025-07-21-capacities-pkm 301
|
||||||
|
/2025-08-07-nptel-underrated /blog/2025-08-07-nptel-underrated 301
|
||||||
|
/2025-08-16-interactive-fiction-textbooks /blog/2025-08-16-interactive-fiction-textbooks 301
|
||||||
|
/2025-08-27-vibecoding-manim-slides /blog/2025-08-27-vibecoding-manim-slides 301
|
||||||
|
/2025-08-29-grok-a-book-new-editions /blog/2025-08-29-grok-a-book-new-editions 301
|
||||||
|
/2025-09-05-pocket-shutdown /blog/2025-09-05-pocket-shutdown 301
|
||||||
|
/2025-09-23-ten-years-iitgn /blog/2025-09-23-ten-years-iitgn 301
|
||||||
|
/2025-11-17-iisc-time-tracking /blog/2025-11-17-iisc-time-tracking 301
|
||||||
|
/2026-02-20-value-of-things-comment /blog/2026-02-20-value-of-things-comment 301
|
||||||
|
/2026-02-25-mattermost-for-openclaw /blog/2026-02-25-mattermost-for-openclaw 301
|
||||||
27
scripts/generate-redirects.js
Normal file
27
scripts/generate-redirects.js
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Generate _redirects file for pretty URLs
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
const contentDir = path.join(__dirname, '../src/content/blog');
|
||||||
|
|
||||||
|
const redirects = [
|
||||||
|
'# redirects for pretty URLs - generated automatically',
|
||||||
|
''
|
||||||
|
];
|
||||||
|
|
||||||
|
// Read all blog post directories
|
||||||
|
const entries = fs.readdirSync(contentDir, { withFileTypes: true });
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
const slug = entry.name;
|
||||||
|
redirects.push(`/${slug} /blog/${slug} 301`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write _redirects file
|
||||||
|
const outputPath = path.join(__dirname, '../public/_redirects');
|
||||||
|
fs.writeFileSync(outputPath, redirects.join('\n') + '\n');
|
||||||
|
|
||||||
|
console.log(`Generated ${redirects.length - 2} redirects`);
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
---
|
|
||||||
export const prerender = true;
|
|
||||||
import { getCollection } from 'astro:content';
|
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
|
||||||
const posts = await getCollection('blog');
|
|
||||||
|
|
||||||
return posts.map((post) => ({
|
|
||||||
params: { id: post.id },
|
|
||||||
props: { slug: post.id },
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
const { slug } = Astro.props;
|
|
||||||
return Astro.redirect(`/blog/${slug}`, 301);
|
|
||||||
---
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue